vendredi 18 novembre 2016

publisher subscriber pattern of zmq is not working as expected

I have simple sample program for pub/sub pattern in nodejs which is as shown below

publisher.js

var zmq = require('zmq');
var pub = zmq.socket('pub');

pub.bind('tcp://127.0.0.1:6666');

console.log("Current 0MQ version is " + zmq.version);
var loop =0

pub.send(['t',loop++ + ' pub msg']);
pub.send(['t',loop++ + ' pub msg']);

subber.js

var zmq = require('zmq');
var sub = zmq.socket('sub');
sub.connect('tcp://127.0.0.1:6666');

sub.subscribe('t');  //herein lies the question
console.log('Received msg:');
sub.on('message',function(topic, msg){
    console.log('Received msg:',msg.toString());
})

there are some unexpected behaviour which i am falling to understand from two days

1:for the first time when i run subber.js first and then publisher.js i will get the messages as expected

2:Again if i run in the same order i am not getting any messages (subber.js and publisher.js)

3: for first i run publisher.js and then subber.js none of the messages i will get

really i am not understanding that sample program working or not please help me to understand publisher subscriber pattern of zmq with some sample code (**In the sample i have not used setinterval bcoz once the publisher send the message i have send to subscriber please provide the some sample to understand and accomplish this)

Thanks

Aucun commentaire:

Enregistrer un commentaire