Last active
July 13, 2024 02:33
-
-
Save jp/4628ddf1a1811f659d25 to your computer and use it in GitHub Desktop.
kafka / node / websocket
This file contains hidden or 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
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