2

I'm using KaafkaJS to connect and consume the kafka messsages. I'm using the following code to consume messages. But it fetches all the messages but I would like to store the last message in the variable messageValue. Can someone help me with it please.

await consumer.run({ 
            eachMessage: async (data) => {
                    messageValue = data.message.value.toString('utf8').trim()
            }
        })
console.log(messageValue)

2 Answers2

0

you just need to add fromBeggining: false

https://kafka.js.org/docs/consuming#a-name-from-beginning-a-frombeginning

Leyb
  • 1
  • 5
0

This below code worked for me. Reference

const consume = async () => {
    await consumer.connect()
    await consumer.subscribe({ topic, fromBeginning: true })
    await consumer.run({
        eachMessage: ({ message }) => { 
            console.log(`received message: ${message.value}`)
        },
    })
}
user2573747
  • 15
  • 1
  • 5