kafka-node-basic-example
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" | |
let kafka = require('kafka-node'), | |
HighLevelProducer = kafka.HighLevelProducer, | |
client = new kafka.Client('localhost:2181/'), | |
producer = new HighLevelProducer(client) | |
// In this demo, topics are intended to be created already. | |
// See kafka docs (or check https://hub.docker.com/r/wurstmeister/kafka/ => | |
// "Automatically create topics") | |
let topicDemo = 'topic-demo' | |
producer.on('ready', () => | |
producer.send([ | |
{ topic: topicDemo, messages: 'hi' } | |
], (err, data) => { | |
console.log('Producer sent some messages. Result:') | |
console.log({ | |
err: err, | |
data: data | |
}) | |
}) | |
) | |
// This code is intended to be in a different application. | |
// I'm leaving it here just for demo purposes | |
let Consumer = kafka.Consumer, | |
consumer = new Consumer( | |
client, | |
[ | |
{ topic: topicDemo, partition: 0 } | |
], | |
{ | |
autoCommit: true | |
} | |
) | |
consumer.on('message', (message) => { | |
console.log('Consumer got a message:') | |
console.log(message) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment