kafkajs-patched-cpu-utilization
Version:
[Patched 2.2.4 version] A modern Apache Kafka client for node.js
58 lines (46 loc) • 1.2 kB
JavaScript
const { Kafka } = require('.')
const consume = async () => {
const kafka = new Kafka({
clientId: 'my-app',
brokers: ['127.0.0.1:9092', '127.0.01:9092'],
})
const consumer = kafka.consumer({ groupId: 'test-group' })
await consumer.connect()
await consumer.subscribe({ topic: 'manypartitions', fromBeginning: true })
await consumer.run({
eachBatch: async ({ batch }) => {
console.log({
batch: batch.messages,
})
},
})
// eachMessage: async ({ topic, partition, message }) => {
// console.log({
// value: message.value.toString(),
// });
// },
// });
}
const produce = async () => {
const kafka = new Kafka({
clientId: 'my-app-producer',
brokers: ['127.0.0.1:9092', '127.0.01:9092'],
})
const producer = kafka.producer()
await producer.connect()
const sendLoop = async () => {
setTimeout(sendFunc, 1)
}
const sendFunc = async () => {
await producer.send({
topic: 'manypartitions',
messages: [{ value: `This is a test message!` }],
})
await sendLoop()
}
sendLoop()
}
// Promise.all([consume(), produce()])
consume()
// produce()
console.log('done')