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
?