I'm attempting to implement dynamic discovery of networked "nodes" using ZMQ's XPub-XSub pattern. Every example I can find online is limited to a single machine - using localhost for the IP address. When I implement them across more than one machine, nothing seems to happen.
I have the following scripts:
(zproxy.py)
import zmqif __name__ == "__main__": context = zmq.Context() # Socket facing producers frontend = context.socket(zmq.XPUB) frontend.bind("tcp://*:5559") # Socket facing consumers backend = context.socket(zmq.XSUB) backend.bind("tcp://*:5560") zmq.proxy(frontend, backend) # We never get here… frontend.close() backend.close() context.term()
(zpub.py)
import zmq, timedef getNetworkIp(): import socket s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) s.connect(('<broadcast>', 12345)) return s.getsockname()[0]if __name__ == "__main__": context = zmq.Context() s = context.socket(zmq.PUB) s.connect("tcp://{}:5560".format(getNetworkIp())) while True: time.sleep(2) t = time.strftime('%H:%M:%S') s.send_string('Time:'+ t) print('sent {}'.format(t)) context.term()
(zsub.py)
import zmqdef getNetworkIp(): import socket s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) s.connect(('<broadcast>', 12345)) return s.getsockname()[0]if __name__ == "__main__": context = zmq.Context() subscriber = context.socket(zmq.SUB) subscriber.connect("tcp://{}:5559".format(getNetworkIp())) subscriber.setsockopt_string(zmq.SUBSCRIBE, "") while True: if subscriber.poll(1000, zmq.POLLIN): message = subscriber.recv() print('{}'.format(message)) context.term()
If I run all three scripts on one machine, of course they all work and I get the subscriber printing output. However if I run the zproxy.py and zpub.py scripts on one machine, and zsub.py on another machine (that I'm remotely connected to, and can ping), the subscriber never prints anything. Attempting to run the zproxy.py script on the second machine (followed by the zsub.py script) also fails.
Can anyone tell me what I'm doing incorrectly?