node.js and zeromq, part 4 (XRequest/XReply)
Today, after a long wait is part 4 of the node.js and ZeroMQ series. This part is about XRequest and XReply sockets also called XREQ and XREP sockets.
There isn't a lot of documentation about this. As I understand it, these socket types allow you to asynchronously implement the request response pattern. Pretty much like REQ/REP actually, but I've found XREQ/XREP to be more stable.
Without further ado, here's xrequestxreplyclient.js:
var zeromq = require("zeromq");
var counter = 0;
var socket = zeromq.createSocket('xrequest');
socket.identity = "client";
socket.connect("tcp://127.0.0.1:5502");
console.log("Connected to port 5502!");
setInterval(function(){
var value = Math.floor(Math.random()*100);
socket.send('', ''+value);
}, 10);
socket.on('message', function(data) {
console.log("Answer data: "+data);
counter += 1;
});
socket.on('error', function(error) {
console.log("Error: "+error);
});
Here's xrequestxreplyserver.js:
var zeromq = require("zeromq");
var socket = zeromq.createSocket('xreply');
socket.identity = "server";
socket.bind("tcp://127.0.0.1:5502", function(err) {
if (err) throw err;
console.log("bound!");
socket.on('message', function(envelope, blank, data) {
console.log("Received: "+envelope+" - "+data.toString('utf8'));
socket.send(envelope, ''+data*2);
});
socket.on('error', function(err) {
console.log("Error: "+err);
});
});
Here again the server part is actually a value duplication service (times two service).
