I have a pub/sub system composed by two VMs (VirtualBOx, Ubuntu 18.04 and python-zmq[16.0.2-2build2]) running within the same physical machine (Win10). Both machines are configured as Bridge.
The subscriber can receive the publisher messages if it is configured with the pub IP, however, it is not scalable with multiple publishers.
PUB (server) is configured as:
import zmq
import random
import sys
import time
port = "5557"
if len(sys.argv) > 1:
port = sys.argv[1]
int(port)
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:%s" % port)
while True:
topic = random.randrange(9999,10005)
messagedata = random.randrange(1,215) - 80
print "%d %d" % (topic, messagedata)
socket.send("%d %d" % (topic, messagedata))
time.sleep(1)
And the SUB (client) as
import sys
import zmq
port = "5557"
if len(sys.argv) > 1:
port = sys.argv[1]
int(port)
if len(sys.argv) > 2:
port1 = sys.argv[2]
int(port1)
# Socket to talk to server
context = zmq.Context()
socket = context.socket(zmq.SUB)
print "Collecting updates from weather server..."
socket.connect ("tcp://192.168.1.66:%s" % port)
if len(sys.argv) > 2:
socket.connect ("tcp://localhost:%s" % port1)
# Subscribe to zipcode, default is NYC, 10001
topicfilter = "10001"
socket.setsockopt(zmq.SUBSCRIBE, topicfilter)
# Process 5 updates
total_value = 0
for update_nbr in range (5):
string = socket.recv()
topic, messagedata = string.split()
total_value += int(messagedata)
print topic, messagedata
print "Average messagedata value for topic '%s' was %dF" % (topicfilter, total_value / update_nbr)
I've tried to configure at client in socket.connect ("tcp://IP:%s" % port):
"*"
Gives the error:
Traceback (most recent call last):
File "sub_client.py", line 18, in <module>
socket.connect ("tcp://*:%s" % port)
File "zmq/backend/cython/socket.pyx", line 528, in zmq.backend.cython.socket.Socket.connect (zmq/backend/cython/socket.c:5980)
File "zmq/backend/cython/checkrc.pxd", line 25, in zmq.backend.cython.checkrc._check_rc (zmq/backend/cython/socket.c:8400)
zmq.error.ZMQError: Invalid argument
192.168.1.1 (GW), 192.168.1.255 (broadcast), localhost/127.0.0.1 and it's IP (192.168.1.55) -> does not receive messages
192.168.1.66 (server's IP) -> Does receive messages but not practical in a large scale system
Any way to solve this?