简体   繁体   中英

ZMQ pub/sub subscribe

I am having trouble figuring out how to subscribe to a particularly "channel" with ZMQ with regard to its pub/sub functionality.

Here is the publisher:

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

pub.bindSync('tcp://127.0.0.1:5555');

setInterval(function(){
    pub.send('pub msg');
},500);

here is the subscriber:

 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);
 }

This works as is, but the problem is that if I change the argument to sub.subscribe to anything but an empty string (''), the subscriber doesn't receive any messages from the publisher.

How do I configure pub/sub with ZMQ correctly?

sub.subscribe('topic') adds a filter to your subscriber socket so that you only receive messages starting with the string topic . You can add multiple filters by calling it more than once. sub.subscribe('') removes any existing filter so your subscriber gets all messages sent by the publisher.

In your code using sub.subscribe('pub') would yield messages on the subscriber side.

The pub/sub example in the zeromq.node GitHub is a good place to look to understand how subscriptions work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM