2
"""Simple example of publish/subscribe illustrating topics.
4
Publisher and subscriber can be started in any order, though if publisher
5
starts first, any messages sent before subscriber starts are lost. More than
6
one subscriber can listen, and they can listen to different topics.
8
Topic filtering is done simply on the start of the string, e.g. listening to
9
's' will catch 'sports...' and 'stocks' while listening to 'w' is enough to
14
# Copyright (c) 2010 Brian E. Granger, Fernando Perez
16
# This file is part of pyzmq.
18
# pyzmq is free software; you can redistribute it and/or modify it under
19
# the terms of the Lesser GNU General Public License as published by
20
# the Free Software Foundation; either version 3 of the License, or
21
# (at your option) any later version.
23
# pyzmq is distributed in the hope that it will be useful,
24
# but WITHOUT ANY WARRANTY; without even the implied warranty of
25
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
# Lesser GNU General Public License for more details.
28
# You should have received a copy of the Lesser GNU General Public License
29
# along with this program. If not, see <http://www.gnu.org/licenses/>.
38
if len (sys.argv) < 2:
39
print 'usage: subscriber <connect_to> [topic topic ...]'
42
connect_to = sys.argv[1]
46
s = ctx.socket(zmq.SUB)
49
# manage subscriptions
51
print "Receiving messages on ALL topics..."
52
s.setsockopt(zmq.SUBSCRIBE,'')
54
print "Receiving messages on topics: %s ..." % topics
56
s.setsockopt(zmq.SUBSCRIBE,t)
60
topic, msg = s.recv_multipart()
61
print ' Topic: %s, msg:%s' % (topic, msg)
62
except KeyboardInterrupt:
66
if __name__ == "__main__":