I am using kafka-node npm module ,0.2.27 version.
I found producer.on('ready',fn(){})
is never getting called.
When I inspected producer object, observed it is as follows
{ 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 on ('ready') event, I just checked if(producer.ready)
and I am able to publish to kafka with a little timeout.
Ideally, event should be triggered.
I am not sure whether I am taking the right approach.
Any pointers in this direction is greatly appreciated.
Thanks in advance
Try 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();
});
I faced this issue too and your approach I believe is the right one. See the ready event of the producer is fired only once, when the constructor of the producer is called. After the producer.ready property is set to true. Since I believe like me, you also were calling the constructor in some other place and returning or exporting the producer from there, it means, that ready event won't be fired in the place where you're actually calling producer.send()
Instead, using
`if(producer.ready){
producer.send(payloads, function(err,res){
console.log(res)
});
}
`
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With