Created
June 9, 2012 22:58
-
-
Save ceejbot/2902934 to your computer and use it in GitHub Desktop.
ZeroMQ router/dealer example for node.js
This file contains 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 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' | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
could you add something to this that shows how to send a message to the router?