简体   繁体   English

ZMQ发布/订阅

[英]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. 我在弄清楚如何在ZMQ的发布/订阅功能方面订阅特别的“频道”方面遇到麻烦。

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. 这按原样工作,但是问题是,如果我将参数sub.subscribe更改为除空字符串('')之外的任何内容,则订阅者不会收到来自发布者的任何消息。

How do I configure pub/sub with ZMQ correctly? 如何正确使用ZMQ配置发布/订阅?

sub.subscribe('topic') adds a filter to your subscriber socket so that you only receive messages starting with the string topic . sub.subscribe('topic')将过滤器添加到订户套接字,以便您仅接收以字符串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. sub.subscribe('')删除任何现有的筛选器,以便您的订阅者获取发布者发送的所有消息。

In your code using sub.subscribe('pub') would yield messages on the subscriber side. 在您的代码中,使用sub.subscribe('pub')会在订阅sub.subscribe('pub')消息。

The pub/sub example in the zeromq.node GitHub is a good place to look to understand how subscriptions work. 在zeromq.node GitHub上的pub / sub示例是了解订阅如何工作的好地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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