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()
})
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
?
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.