Skip to content

Instantly share code, notes, and snippets.

@jp
Last active September 18, 2022 00:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jp/4628ddf1a1811f659d25 to your computer and use it in GitHub Desktop.
Save jp/4628ddf1a1811f659d25 to your computer and use it in GitHub Desktop.
kafka / node / websocket
var WebSocketServer = require('ws').Server
, wss = new WebSocketServer({ port: 8080 });
wss.broadcast = function broadcast(data) {
wss.clients.forEach(function each(client) {
client.send(JSON.stringify(data));
});
};
var kafka = require('kafka-node'),
Producer = kafka.Producer,
Consumer = kafka.Consumer,
client = new kafka.Client("localhost:2181", "topic-test"),
consumer = new Consumer(
client,
[
{ topic: 'test', partition: 0 }
//, { topic: 't1', partition: 1 }
],
{
autoCommit: false
}
);
consumer.on('message', function (message) {
console.log(message);
wss.broadcast(message);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment