Tampa
Tampa

Reputation: 78234

python, zeromq and pub/sub - remote sub is not getting data

On my publisher I use the below to send data.

context_forward2 = zmq.Context()
forward2_socket = context_forward2.socket(zmq.PUB)
forward2_socket.bind("tcp://127.0.0.1:7002")
transpport = 'orderbook stuff'
forward2_socket.send(transpport)

On my subscriber server...

context = zmq.Context()
forward_socket = context.socket(zmq.SUB)
forward_socket.connect("tcp://89.55.55.55:7002")
forward_socket.setsockopt(zmq.SUBSCRIBE, 'orderbook')
time.sleep(5)
while True:
    print 'a'
    transpport = forward_socket.recv()
    print 'b'
    transpport = transpport.split('orderbook ')[1]
    print 'c'
    print transpport

But issue is that is just hangs at transpport = forward_socket.recv()

Why will zeromq not work?

Upvotes: 0

Views: 1227

Answers (2)

cronburg
cronburg

Reputation: 891

Add time.sleep(5) right before the socket.send() in the publisher. You need to establish a connection between publishers and subscribers before transmitting data. Your issue is a timing issue.

The following programs work as expected for me:

Publisher:

import zmq 
import time
context_forward2 = zmq.Context()
forward2_socket = context_forward2.socket(zmq.PUB)
forward2_socket.bind("tcp://127.0.0.1:7002")
transpport = 'orderbook stuff'
time.sleep(5)
forward2_socket.send(transpport)

Subscriber:

import zmq 
import time
context = zmq.Context()
forward_socket = context.socket(zmq.SUB)
forward_socket.connect("tcp://127.0.0.1:7002")
forward_socket.setsockopt(zmq.SUBSCRIBE, 'orderbook')
time.sleep(5)
while True:
  print 'a' 
  transpport = forward_socket.recv()
  print transpport
  print 'b' 
  transpport = transpport.split('orderbook ')[1]
  print 'c' 
  print transpport

(with your static IP address replaced with 127.0.0.1 because I'm testing on a local machine)

Upvotes: 0

kev
kev

Reputation: 161604

If you want outside world to connect in. You need to bind 89.55.55.55 or 0.0.0.0 (but not 127.0.0.1, it only listen local tcp package)

Upvotes: 2

Related Questions