25 lines
560 B
JavaScript
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() |