nodeJS zeroMQ: why cant send message if sock.send not in setInterval

I was using zeroMQ in nodeJS. But it seems that while sending the data from producer to worker, if I do not put it in setInterval, then it does not send the data to the worker. My example code is as follows:

producer.js
===========           
     var zmq = require('zmq')
      , sock = zmq.socket('push');

    sock.bindSync('tcp://127.0.0.1:3000');
    console.log('Producer bound to port 3000');
    //sock.send("hello");
    var i = 0;

     //1. var timer = setInterval(function() {
      var str = "hello";
      console.log('sending work', str, i++);

     sock.send(str);
     //2. clearTimeout(timer);
     //3. }, 150);

    sock.on('message', function(msg) {
    console.log("Got A message, [%s], [%s]", msg);
   });

So in the above code, if I add back the lines commented in 1, 2 and 3, then I do receive the message to the worker side, else it does not work.

Can anyone throw light why to send message I need to put it in setInterval? Or am I doing something wrong way?

The problem is hidden in the zmq bindings for node.js . I've just spent some time digging into it and it basically does this on send():

  1. Enqueue the message
  2. Flush buffers

Now the problem is in the flushing part, because it does

  1. Check if the output socket is ready, otherwise return
  2. Flush the enqueued messages

In your code, because you call bind and immediately send, there is no worker connected at the moment of the call, because they simply didn't have enough time to notice. So the message is enqueued and we are waiting for some workers to appear. Now the interesting part - where do we check for new workers? In the send function itself! So unless we call send() later, when there are actually some workers connected, our messages are never flushed and they are enqueued forever. And that is why setInterval works, because workers have enough time to notice and connect and you periodically check if there are any.

You can find the interesting part at https://github.com/JustinTulloss/zeromq.node/blob/master/lib/index.js#L277 .

Cheers ;-)