1

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);
                })
dhana lakshmi
  • 847
  • 1
  • 12
  • 29
  • You need to subscribe to the sending event. In your case, you are subscribed to nothing ( sub.subscribe(''") ). Subscribe: sock.subscribe('kitty cats'); Publish: sock.send(['kitty cats', 'meow!']); 'kitty cats' is the keyword here :) – Amiga500 Nov 16 '16 at 10:22
  • when we dont specify it means get all the message so only please say with prospect to the sample what should be the value of sub.subscribe(''") – dhana lakshmi Nov 16 '16 at 10:37
  • You might be correct. But in your case, I would set a message identifier in any case. – Amiga500 Nov 16 '16 at 10:38
  • i am not sure how to subscribe is there a doc where i can refer and get it done – dhana lakshmi Nov 16 '16 at 10:39

1 Answers1

0

Based on this code, from their documentaiton:

// pubber.js 
var zmq = require('zmq')
   , sock = zmq.socket('pub');

  sock.bindSync('tcp://127.0.0.1:3000');
 console.log('Publisher bound to port 3000');

setInterval(function(){
  console.log('sending a multipart message envelope');
  sock.send(['kitty cats', 'meow!']);
}, 500);

It seems that setInterval is just utility function :)

What is preventing that you fire it manually?

So for example, imaginary controller:

exports.animals= {
   getCat: function (request, response) {

   var animal = request.payload.catStuff;

   //process the animal object
  //and manually fire the message like:


sock.send(['kitty cats', 'meow!']);

...
Amiga500
  • 5,874
  • 10
  • 64
  • 117
  • i am trying to build a module which involves no timer when ever a message is published the subscriber should get it – dhana lakshmi Nov 16 '16 at 09:46
  • thanks yes i understood it is just a utility i have a route to publish the message router.post('/putMsgIn0MQ', function (req, res, next) { pushData(queueItem,JSON.stringify(req.body)) }) which avoid the automatic timmer the entire pubber.js is in pushData but subscriber is not receving the message not sure why is that – dhana lakshmi Nov 16 '16 at 10:06
  • Please post the code you have so far, lets take a look :) – Amiga500 Nov 16 '16 at 10:07