[zeromq-dev] Changing subscriber's topic

Chuck Remes cremes.devlist at mac.com
Fri Jul 30 07:08:25 CEST 2010


On Jul 29, 2010, at 10:50 PM, Eric Bell wrote:

> What is the behavior when subscriber in a publish-subcribe pattern changes the message topic (aka message type) it is subscribed to?
> 
> I ran a test where I changed the message topic and then repeatedly read all messages from the socket until an exception was thrown. The messages that were read were from both the previous topic and the new topic.  Here is a snippet of my python code:
> 
>     if topic=='b':
>         topic = 'g'
>     else:
>         topic = 'b'
> 
>     socket.setsockopt(zmq.SUBSCRIBE, topic)
>     status = ''
> 
>     while True:
>         try:
>             event = socket.recv(zmq.NOBLOCK)
>             status = event + '\n' + status
>         except:
>             if status == '':
>                 status = 'no message received'
>             break
> 
> When the loop terminates, the variable "status" contains messages that start with both "b" and "g". This doesn't seem like desirable behavior, but what isn't clear to me is whether the queue should return all messages that have been queued of the new topic since the socket was created, or only new messages that arrive once the topic has been changed ... but not some messages of one topic and some messages of another topic.

Eric,

I think you misunderstand how SUB sockets work. It's possible to subscribe to multiple topics (see http://api.zeromq.org/zmq_setsockopt.html) as you do in the example above.

If you want to stop receiving messages for a topic, call setsockopt with UNSUBSCRIBE (also described at the link), otherwise subsequent calls to SUBSCRIBE will just add *another* filter to the socket and return all messages that match *any* filter.

cr




More information about the zeromq-dev mailing list