Skip to content

Instantly share code, notes, and snippets.

@cantoniazzi
Created April 24, 2018 00:56
Show Gist options
  • Save cantoniazzi/6b93708aae0b0eda39309c1c4ef12887 to your computer and use it in GitHub Desktop.
Save cantoniazzi/6b93708aae0b0eda39309c1c4ef12887 to your computer and use it in GitHub Desktop.
A kafka client in nodejs.
'use strict';
const uuidv4 = require('uuid/v4');
const kafka = require('kafka-node'),
Producer = kafka.Producer,
client = new kafka.KafkaClient({kafkaHost: 'KAFKA_HOST_HERE'}),
producer = new Producer(client);
producer.on("ready", function () {
console.log("Kafka Producer is connected and ready.");
});
module.exports = function kafkaClient(scheduling, cb) {
let _guid = uuidv4();
producer.send([{topic: 'KAFKA_TOPIC_HERE', key: _guid, messages: scheduling}], function (err, data){
if (err){
console.log('error kafka: ', err);
return cb(err, null);
} else {
return cb(null, data);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment