Kafka-node ready event not firing

I am using kafka-node npm module, version 0.2.27.
I found it producer.on('ready',fn(){})

never gets called.
When I examined the producer object, it noticed the following:

{ ready: true,
  client: 
   { connectionString: '10.196.160.100.:2181,10.196.160.150:2181,10.196.160.151:2181',
     clientId: 'dev',
     zkOptions: { sessionTimeOut: 10000, spinDelay: 1000, retries: 10 },
     brokers: 
      { 'custom-kafka.mine.com:9092': [Object],
        'custom-storm.mine.com:9092': [Object] },
     longpollingBrokers: {},
     topicMetadata: {},
     topicPartitions: {},
     correlationId: 0,
     cbqueue: {},
     brokerMetadata: { '0': [Object], '1': [Object] },
     ready: true,
     zk: { client: [Object], _events: [Object], inited: true },
     _events: { ready: [Object], error: [Object], close: [Object] } },
  requireAcks: 1,
  ackTimeoutMs: 100,
  _events: {} }

      

Instead of waiting for the on ('ready') event, I just checked if(producer.ready)

and I can post it to kafka with a little timeout.
Ideally, the event should be fired.
I'm not sure if I'm taking the correct approach.
Any pointers in this direction are greatly appreciated.
thanks in advance

+3


source to share


1 answer


Try using the following code:



var kafka = require('kafka-node'),
    Producer = kafka.Producer,
    client = new kafka.Client('192.168.50.252:2181'),
    producer = new Producer(client),
    payloads = [
        { 
            topic: 'test topic', 
            messages: ['test message'] 
        }
    ];

    client.on('ready', function (){
        console.log('client ready');
    })  

    client.on('error', function (err){
        console.log('client error: ' + err);
    })  

    producer.on('ready', function () {
        producer.send(payloads, function (err, data) {
            console.log('send: ' + data);        
            process.exit();
        });
    });

    producer.on('error', function (err) {
        console.log('error: ' + err);
        process.exit();
    });

      

+3


source







All Articles