Skip to content

Instantly share code, notes, and snippets.

@groundwater
Created October 27, 2012 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groundwater/3966362 to your computer and use it in GitHub Desktop.
Save groundwater/3966362 to your computer and use it in GitHub Desktop.
ZeroMQ Async Weirdness
var zmq = require('zmq')
function sockit(name){
var sock = zmq.socket('req');
sock.connect('tcp://127.0.0.1:9000');
sock.on('message', function(res){
console.log("%s: %s",name,res.toString())
});
return sock;
}
sockit('one').send( JSON.stringify({timeout:5000,name:'one'}) )
sockit('two').send( JSON.stringify({timeout:1000,name:'two'}) )
sockit('thr').send( JSON.stringify({timeout:0,name:'thr'}) )
14:23:20 server.1 | Received: one
14:23:25 server.1 | Received: two
14:23:25 client.1 | one: one
14:23:26 server.1 | Received: thr
var zmq = require('zmq')
var sock = zmq.socket('rep');
sock.bindSync('tcp://127.0.0.1:9000');
sock.on('message', function(res){
var message = JSON.parse(res)
console.log("Received:",message.name)
var timeout = message.timeout;
var name = message.name;
setTimeout(function(){
sock.send(name);
},timeout);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment