Created
April 11, 2017 12:35
-
-
Save trahloff/2843250233cf96b7ab1348f5b5b71051 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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