[zeromq-dev] fire up new subscribers

andrea crotti andrea.crotti.0 at gmail.com
Tue Aug 14 14:00:00 CEST 2012


2012/8/14 andrea crotti <andrea.crotti.0 at gmail.com>:
> I wanted to try now the XPUB pattern and just as a comment, I can't
> find any example about how to use it, and the only documentation is in
> the API reference, where it doesn't actually say that much..
>
> Maybe an example would be nice to have in the guide or in the examples
> directory, what do you think?

This for example doesn't work, because the setsockopt is not compatible:
Traceback (most recent call last):
  File "xsub.py", line 21, in <module>
    sock.setsockopt(zmq.SUBSCRIBE, '')
  File "socket.pyx", line 324, in zmq.core.socket.Socket.setsockopt
(zmq/core/socket.c:3203)
zmq.core.error.ZMQError: Invalid argument

But if I remove the setsockopt the subscriber hangs there and doesn't
receive anything..

import sys
import zmq

PORT = 'ipc://sock'


if __name__ == '__main__':
    context = zmq.Context()
    if len(sys.argv) > 1:
        sock = context.socket(zmq.XPUB)
        sock.bind(PORT)

        while True:
            sock.send('Message to send')
            msg = sock.recv()
            print("Got message from subscribers %s" % msg)

    else:
        sock = context.socket(zmq.XSUB)
        sock.connect(PORT)
        sock.setsockopt(zmq.SUBSCRIBE, '')

        broad = sock.recv()
        print("got broadcast message %s" % broad)
        # now should answer something say it's subscribed somehow
        sock.send('hello world')



More information about the zeromq-dev mailing list