node.js and zeromq (Push/Pull)
While trying to evaluate zeromq and it's use with node.js, I quickly put together the following small experiment, which shows two unidirectional sockets. One is a "server" and can only push (send) messages, the other is a "client", which can only pull (receive) messages:
pipeline_server.js:
var zeromq = require("zeromq");
// push = downsteam = send only
var socket = zeromq.createSocket('push');
socket.bind("tcp://127.0.0.1:12345", function(err) {
if (err) throw err;
console.log("bound!");
setInterval(function() {
socket.send(new Date().toString());
}, 500);
});
The server creates the push socket, connects to port 12345 of localhost and sends a message every half a second (containing the current date/time).
pipeline_client.js:
var zeromq = require("zeromq");
// pull = upstream = receive only
var socket = zeromq.createSocket('pull');
socket.connect("tcp://127.0.0.1:12345");
console.log("connected!");
socket.on('message', function(data) {
console.log("received data: " + data.toString('utf8'));
});
Mind you that these specific socket types are used in the pipeline pattern. The pushing socket will load-balance requests over the pulling sockets. If you start more clients, you'll see that the messages are fairly distributed over all clients.
