ZMQ pub/sub subscribe

10,178

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.

Share:
10,178
Alexander Mills
Author by

Alexander Mills

Dev, Devops, soccer coach. https://www.github.com/oresoftware

Updated on August 20, 2022

Comments

  • Alexander Mills
    Alexander Mills over 1 year

    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?