I am trying to implement pub/sub as shown below
publisher.js
var zmq = require('zmq');
var pub = zmq.socket('pub');
pub.bindSync('tcp://127.0.0.1:5555');
pub.send('pub msg');
/*
setInterval(function(){
console.log("sending message")
},500);*/
subscriber.js
var zmq = require('zmq');
var sub = zmq.socket('sub');
sub.connect('tcp://127.0.0.1:5555');
sub.subscribe(''); //herein lies the question
sub.on('message',function(msg){
console.log('Received msg:',msg);
})
the above subscriber will receive the message only when pub.send('pub msg');
is inside setInterval not sure about my understanding
I dont want use setInterval rather i have to send the message as soon it arrives
Please say how can i do it using pub/sub only i guess there is some basic understanding missing please help
in nodejs code i am trying using a route has
router.post('/putMsgIn0MQ', function (req, res, next) {
pushData(JSON.stringify(req.body))
})
var pushData = function(dataToPush) {
var zmqSocket = zmq.socket('pub')
var zmqPortPart = 'tcp://127.0.0.1:5555'
zmqSocket.bind(zmqPortPart);
zmqSocket.send(dataToPush);
}
subscriber
var sub = zmq.socket('sub');
sub.connect('tcp://127.0.0.1:5555');
sub.subscribe(''); //herein lies the question
console.log('Received msg:');
sub.on('message',function(msg){
console.log('Received msg:');
console.log(msg.toString())
var jsonPayload = msg.toString();
processData(jsonPayload, zmqPortObj.name);
})