kafkajs/consumer.js
2023-05-26 10:07:44 -04:00

25 lines
560 B
JavaScript

const { Kafka } = require('kafkajs')
const kafka = new Kafka({
clientId: 'my-app',
brokers: ['localhost:9092']
})
const consumer = kafka.consumer({ groupId: 'test-group' })
const run = async () => {
// Consuming
await consumer.connect()
await consumer.subscribe({ topic: process.env.TOPIC, fromBeginning: true })
await consumer.run({
eachMessage: async ({ topic, partition, message }) => {
const obj = JSON.parse(message.value)
console.log('Message consumer successfully!');
console.log(obj.name)
},
})
}
run()