I an running kafka broker on my windows 10 machine:
[2023-06-19 19:12:33,360] INFO Registered kafka:type=kafka.Log4jController MBean (kafka.utils.Log4jControllerRegistration$)
3.4.1 (Commit:8a516edc2755df89)
I am running zookeeper and kafka server both using bin\windows\
executables.
When I run
kafka-console-producer.bat --topic test --bootstrap-server localhost:9092
And
kafka-console-consumer.bat --topic test --bootstrap-server localhost:9092 --from-beginning
both are working fine and I am able to send messages from producer to consumer.
But when I try to producer using nodejs it gives me below error.
{"level":"WARN","timestamp":"2023-06-19T13:30:50.265Z","logger":"kafkajs","message":"KafkaJS v2.0.0 switched default partitioner. To retain the same partitioning behavior as in previous versions, create the producer with the option \"createPartitioner: Partitioners.LegacyPartitioner\". See the migration guide at https://kafka.js.org/docs/migration-guide-v2.0.0#producer-new-default-partitioner for details. Silence this warning by setting the environment variable \"KAFKAJS_NO_PARTITIONER_WARNING=1\""}
{"level":"ERROR","timestamp":"2023-06-19T13:30:50.304Z","logger":"kafkajs","message":"[Connection] Connection error: connect ECONNREFUSED ::1:9092","broker":"localhost:9092","clientId":"my-app","stack":"Error: connect ECONNREFUSED ::1:9092\n at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16)"}
Code is:
const { Kafka } = require('kafkajs')
const kafka = new Kafka({
clientId: 'my-app',
brokers: ['localhost:9092']
})
const producer = kafka.producer()
const run = async () => {
// Producing
await producer.connect()
await producer.send({
topic: 'test',
messages: [
{ value: 'Hello KafkaJS user!' },
],
})
// Consuming
await consumer.connect()
await consumer.subscribe({ topic: 'test-topic', fromBeginning: true })
await consumer.run({
eachMessage: async ({ topic, partition, message }) => {
console.log({
partition,
offset: message.offset,
value: message.value.toString(),
})
},
})
}
run().catch(console.error);
I tried using kafka-node and kafka npm packages but getting same error.
I have added below lines in server.properties file
listeners=PLAINTEXT://localhost:9092
but still no success.
I am expectng a solution which will work with best kafka node package for high scalibility.
I am using this link: [https://www.npmjs.com/package/kafkajs]