2

I have two .js files - pub.js for publication and sub.js for subscription. These files are actually a 'split' version of the example shown on node-nanomsg GitHub site. The pub.js writes to tcp://127.0.0.1:7789 and sub.js reads from the same. I start the sub.js first followed by pub.js. While the pub.js completes quickly, the sub.js never receives the message.

pub.js

var nano = require('nanomsg')
var pub = nano.socket('pub')
pub.bind('tcp://127.0.0.1:7789')
//
pub.send('Hello')
pub.close()

sub.js

var nano = require('nanomsg')
var sub = nano.socket('sub')
sub.connect('tcp://127.0.0.1:7789')
//
sub.on('data', function(buf) {
    console.log(String(buf))
    sub.close()
})

UPDATE

If pub.js is written as below and sub.js started first, then the communication goes through.

var nano = require('nanomsg')
var pub = nano.socket('pub')
pub.bind('tcp://127.0.0.1:7789')
//
setTimeout(() => {
    pub.send('Hello')
},2000)
//pub.close()

But, a pub-sub paradigm does not require the publishers and subscribers to be aware of each other. How do I enable a pub-sub system with nanomsg?

cogitoergosum
  • 2,309
  • 4
  • 38
  • 62
  • Doesn't make much sense. Are you sure you didn't start `pub.js` first in your original example? The only difference between the original code and the Update is that you add a timeout to `pub.js`. What if you just wait 2 seconds in your original example and then start `pub.js` – mihai Nov 20 '18 at 11:14
  • Well that's the question. It seems to work with a timeout only. – cogitoergosum Nov 20 '18 at 11:59

1 Answers1

0

It seems like the communication is closed by the publisher before binding the tcp socket completes.

The library doesn't provide async versions of bind and connect, and it seems like bind doesn't completely finish executing when the function returns. Adding a timeout is just a workaround for this problem.

See this discussion on their Github repository: rewrite bind/connect and remove setTimeouts from tests
Unfortunately the pull request got closed and the async feature is still not implemented.

If you look at the examples they provide, they all rely on timeouts, which means the problem is acknowledged by the developers. In my opinion this library is not ready for production use, because of this reliance on timeouts.

mihai
  • 37,072
  • 9
  • 60
  • 86
  • Thank you, for the explanation. I think, this issue exists with [Ømq](http://zguide.zeromq.org/js:_start) also. – cogitoergosum Nov 20 '18 at 14:57
  • Not sure, I see that 0mq provides both `bind` and `bindSync`: https://github.com/zeromq/zeromq.js/blob/master/lib/index.js#L435 – mihai Nov 20 '18 at 15:05