narcisoguillen / kafka-node-avro

ISC License
26 stars 13 forks source link

Is it possible for Consumer to get the lastest 10 messages ? #12

Closed FabriceAdeo closed 4 years ago

FabriceAdeo commented 4 years ago

Hi,

Is it possible for Consumer to get the lastest 10 messages ?

Thank you

narcisoguillen commented 4 years ago

Hi, so once the message is committed on a consumer group its "gone" , if you want to start your code and see the same messages again ( not commit them ) then you can


KafkaAvro.init(Settings).then( kafka => {

  let myConsumer = kafka.addConsumer("my.topic", { autoCommit : false });

  myConsumer.on('message', message => {
    console.log(message);
  });

  myConsumer.on('error', error => {
    console.log(error);
  });

} , error => {
  console.log(error);
});

you can play around with the consumerGroup options like groupId, fromOffset, commitOffsetsOnFirstJoin and onRebalance

once you are done and want to commit the messages you can , like :

  myConsumer.consumer.commit(true, error => {
    console.log(error);
  });