Nodejs ZMQ monitoring sockets

1.3k views Asked by At

I got this code for monitoring sockets in the zmq bindings for nodejs. So far it works but my problem is I dnt know what events the monitoring socket has. The code I got only did that, I will continue looking for more code but this is what I have so far.. ``

var zmq = require('zmq');
var socket = zmq.socket('pub');
socket.connect('tcp://127.0.0.1:10001');
socket.monitor();

I tried adding an "onmessage" event handler but it showed nothing, so.. I dnt know whats up..

socket.on("message",function(msg){
    console.log(msg);
});
2

There are 2 answers

1
akaphenom On

I haven't worked with the PUB/SUB handlers in 0mq. I have used some of the other types and am fairly familiar. Having not tested this code, my recommendation would be

SCRIPT 1: Your existing PUB script, needs to send a message

  socket.send('TEST_MESSAGES', 'BLAH')

SCRIPT 2: This needs to be added:

var zmq = require('zmq');
var sub_socket = zmq.socket('sub');
sub_socket.connect('tcp://127.0.0.1:10001');
sub_socket.subscribe('TEST_MESSAGES')
sub_socket.on("message",function(msg){
    console.log(msg);
});

The trick here is timing. 0mq doesn't give you retries or durable messages. You need to build those elements on your own. Still if you put your publish in a timer (for the sake of getting an example running) you should see the messages move.

0
Phillip Ochola On

I printed the object that I got back from the monitor() function and from it I was able to get some monitor events, I think it is unelegant though, I got this link that tests the monitor function of the socket ( https://github.com/JustinTulloss/zeromq.node/blob/master/test/socket.monitor.js ) but some things are not working but...

mon.monitor();
console.log(mon);
mon.on("message",function(msg){
    console.log(msg);
});
mon.on('close',function(){console.log("Closed");});
mon._zmq.onMonitorEvent = function(evt){
    if (evt == 1)
        console.log("Should be 1 : "+ evt);
    else
        console.log(evt);
};