Hello,
I am trying to use kafka-node to produce and send messages. The message is produced via a POST api call.
I am trying to avoid calling this code each time I get a post call as this will reinitiate the connection to Kafka multiple times. As you can see in the code, there is a payloads section which I get from the API call each time I this endpoint gets called.
I dont want to put the complete code here as it may take up a lot more space. If I am not clear enough, please let me know. I will try to refactor the code and paste it here.
var kafka = require('kafka-node'),
HighLevelProducer = kafka.HighLevelProducer,
KeyedMessage = kafka.KeyedMessage,
client = new kafka.Client("localhost:2181"),
producer = new HighLevelProducer(client),
km = new KeyedMessage('key', 'message'),
payloads = [ {topic: topic,messages: bodyfd} ];
If I declare it at the beginning of the code, my api call hangs but if I declare it in the event loop, the message gets written to kafka topic and API returns a good response.