Skip to content

Instantly share code, notes, and snippets.

@trahloff
Created April 11, 2017 12:35
Show Gist options
  • Save trahloff/2843250233cf96b7ab1348f5b5b71051 to your computer and use it in GitHub Desktop.
Save trahloff/2843250233cf96b7ab1348f5b5b71051 to your computer and use it in GitHub Desktop.
'use strict'
const kafka = require('kafka-node')
const client = new kafka.Client('kafka_broker:2181')
const HighLevelProducer = kafka.HighLevelProducer
const producer = new HighLevelProducer(client)
let prodRdy = false
producer.on('ready', () => {
producer.createTopics(['test'], false, function (err, data) {
if (!err) {
console.log('producer connected and ready\ncreated "test" topic')
prodRdy = true
} else {
console.error(err)
}
})
})
exports.send = (messages, callback) => {
const payloads = [ { topic: 'test', messages: messages } ]
if (prodRdy) {
producer.send(payloads, (err, data) => {
callback(err, data)
})
} else {
callback('producer not ready', null)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment