Skip to content

Instantly share code, notes, and snippets.

@ceejbot
Created June 9, 2012 22:58
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ceejbot/2902934 to your computer and use it in GitHub Desktop.
Save ceejbot/2902934 to your computer and use it in GitHub Desktop.
ZeroMQ router/dealer example for node.js
var zmq = require('zmq');
function Majordomo(options)
{
this.requests = zmq.socket('router');
this.requests.identity = 'majordomo:incoming:' + process.pid;
this.responders = zmq.socket('dealer');
this.responders.identity = 'majordomo:outgoing:' + process.pid;
if (options)
this.configure(options);
}
Majordomo.prototype.configure = function(options)
{
var self = this;
this.config = options;
this.requests.on('message', function()
{
var argl = arguments.length,
envelopes = Array.prototype.slice.call(arguments, 0, argl - 1),
payload = arguments[argl - 1];
console.log('incoming request: ' + payload.toString('utf8'));
self.responders.send([envelopes, payload]);
});
this.requests.bind(options['router'], function(err)
{
if (err) console.log(err);
console.log("router on "+options['router']);
});
this.responders.on('message', function()
{
var argl = arguments.length,
envelopes = Array.prototype.slice.call(arguments, 0, argl - 1),
payload = arguments[argl - 1];
console.log('incoming response: ' + payload.toString('utf8'));
self.requests.send([envelopes, payload]);
});
this.responders.bind(options['dealer'], function(err)
{
if (err) console.log(err);
console.log("dealer on "+options['dealer']);
});
}
var majordomo = new Majordomo();
majordomo.configure({
router: 'tcp://127.0.0.1:3009',
dealer: 'tcp://127.0.0.1:3010'
});
@jcollum-hcg
Copy link

could you add something to this that shows how to send a message to the router?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment