Skip to content

Instantly share code, notes, and snippets.

@erkiesken
Created January 14, 2015 16:22
Show Gist options
  • Save erkiesken/48a45fee967ac9dd44d3 to your computer and use it in GitHub Desktop.
Save erkiesken/48a45fee967ac9dd44d3 to your computer and use it in GitHub Desktop.
Node cluster IPC with ZMQ
brew install zeromq
npm install debug blocked zmq
DEBUG=* node zmq-test.js
var cluster = require("cluster");
var blocked = require("blocked");
var debug = require("debug");
var zmq = require("zmq")
var sock;
var ZMQ_URI = "tcp://127.0.0.1:6601";
// var ZMQ_URI = "ipc://zmq-node-test";
if (cluster.isMaster) {
debug = debug("master-"+process.pid);
cluster.fork();
sock = zmq.socket("push");
sock.bindSync(ZMQ_URI);
cluster.on("online", function(worker) {
debug("worker " + worker.process.pid + " came online");
});
cluster.on("exit", function(worker) {
debug("worker " + worker.process.pid + " died");
});
setInterval(function ()
{
debug("starting data send");
sock.send(new Buffer(1024*1024*10).toString("ascii"));
debug("sent data to zmq");
}, 500);
}
else if (cluster.isWorker)
{
debug = debug("worker-" + process.pid);
sock = zmq.socket("pull");
sock.connect(ZMQ_URI);
sock.on("message", function (msg) {
debug("received msg of %s bytes", msg.length);
});
}
blocked(function (ms) {
debug("!! BLOCKED for %sms", ms | 0);
});
debug("started process");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment