Getting connection timeout error while using Kafka-Node module

I am trying to use kafka-node to integrate our application with Kafka. I took the sample code from the plugin site and tried to modularize it. I am using confluent Kafka docker images in the background.

the sample code:

var kafka = require('kafka-node'),
  Producer = kafka.Producer,
  client = new kafka.KafkaClient(),
  producer = new Producer(client);
  producer.on('ready', function () {

producer.on('error', function (err) { })
exports.producer = producer

on the other hand using the following code to access this

var { producer } = require('./test');
function writeToQueue(text, sender, channel_id, type) {
  message = new Message(text, sender, channel_id, type)

  payloads = [
    { topic: 'chat', messages: JSON.stringify(message), partition: 0 }

  producer.send(payloads, function (err, data) {


everything seems to be working fine as the message is getting written to the Kafka topic, the only problem is after sometime, I get the following error

      throw er; // Unhandled 'error' event
TimeoutError: Connection timeout of 10000ms exceeded
    at new TimeoutError (G:\projects\nodejs\messaging\node_modules\kafka-node\lib\errors\TimeoutError.js:6:9)
    at Timeout.<anonymous> (G:\projects\nodejs\messaging\node_modules\kafka-node\lib\kafkaClient.js:253:13)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7)
Emitted 'error' event on Producer instance at:
    at KafkaClient.<anonymous> (G:\projects\nodejs\messaging\node_modules\kafka-node\lib\baseProducer.js:101:10)
    at KafkaClient.emit (events.js:315:20)
    at G:\projects\nodejs\messaging\node_modules\kafka-node\lib\kafkaClient.js:165:16
    at G:\projects\nodejs\messaging\node_modules\async\dist\async.js:3888:9
    at G:\projects\nodejs\messaging\node_modules\async\dist\async.js:473:16
    at iterateeCallback (G:\projects\nodejs\messaging\node_modules\async\dist\async.js:988:17)
    at G:\projects\nodejs\messaging\node_modules\async\dist\async.js:969:16
    at G:\projects\nodejs\messaging\node_modules\async\dist\async.js:3885:13
    at G:\projects\nodejs\messaging\node_modules\kafka-node\lib\kafkaClient.js:207:9
    at G:\projects\nodejs\messaging\node_modules\async\dist\async.js:969:16
PS G:\projects\nodejs\messaging>

I am totally at loss as my knowledge of nodejs has completely exhausted while trying to fix this (which is not much BTW).

Read more here:

Content Attribution

This content was originally published by Vivek at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: