Skip to content

Instantly share code, notes, and snippets.

@joseconstela
Created August 16, 2016 23:22
Show Gist options
  • Save joseconstela/b3d4dd821f68250f93a382f0f437c617 to your computer and use it in GitHub Desktop.
Save joseconstela/b3d4dd821f68250f93a382f0f437c617 to your computer and use it in GitHub Desktop.
kafka-node-basic-example
"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