2
votes

I'm new with node-js programming, so perhaps I'm not seeing something obvious. A receive in zmq is as follows:

socket.on('message', function()
{
     console.log("Message received: ");
     console.log(arguments);
});

How do I put in a zmq.Poll, or receive with the zmq.NOBLOCK flag set, or close the socket with linger=0 after the timeout?

Links to any documentation will be appreciated. The zmq guide example on polling, just use the asynchronous callback and not the poller or the timeout. Is it just a setTimeout and a socket.close()?

I used something like:

var messageReceived = false;
s.on('message', function() { console.log(arguments); messageReceived=true; });                                                                                

setTimeout(function() {
            if (!messageReceived)
            {
                console.log("Nothing received. Exiting...");
                s.close();
            }   
        }, 5000);

Which works. But I'm not sure if this is the recommended method. Also, how safe is node-zeromq for production?

1

1 Answers

1
votes

You get a few things "for free" when you use zeromq with node. Specifically, since node is built from the ground up to not block and use asynchronous methods, poll() becomes redundant, as does NOBLOCK. I ran into the same confusion when I started with this, they just aren't exposed in the node binding and it seemed that they should be until I understood that the binding was built to work the "node way" and as such they weren't needed.

Your method for timing out is fine, so far as it goes; it just depends on what you need, but that's more or less where I'd start.