简体   繁体   中英

ZeroMQ Python Subscription works, NodeJS Subscription does not on same publisher

I'm trying to subscribe to a pub-sub server. When doing that using Python there is no problem, it works like expected. But when I'm trying to subscribe to the exact same server with NodeJS ZMQ nothing happens. I can't figure out where something goes wrong, probably at the subscribe-part?

Python:

from gzip import GzipFile
from cStringIO import StringIO
import zmq

context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect("tcp://127.0.0.1:6701")


socket.setsockopt(zmq.SUBSCRIBE, '')

while True:
    multipart = socket.recv_multipart()
    address = multipart[0]
    contents = ''.join(multipart[1:])
    contents = GzipFile('','r',0,StringIO(contents)).read()
    print("[%s] %s\n" % (address, contents))

socket.close()
context.term()

NodeJS:

var zmq = require('zmq')
  , sock = zmq.socket('sub');

sock.connect('tcp://127.0.0.1:6701');
sock.subscribe('');
console.log('Subscriber connected to port 6701');

sock.on('message', function(topic, message) {
  console.log('received a message related to:', topic, 'containing message:', message);
});

The on-message part in the NodeJS example never gets fired. When I run a simple NodeJS publisher the subscriber works like expected.

Note that the address I connect to is a local IP due to the fact that I run some local distribution tool for the ZeroMQ messages.

What can be the difference between the two scripts that the NodeJS script does not behave like the Python script on the same publishing source?

Both are using ZeroMQ 4.

Edit: As suggested by Jason I would post some the code of the publisher. But because this is a 3rd party I don't have the code of that. But when I fire up a simple Python publisher the subscribers act the same like they do on the 3rd party publisher.

Simple Python publisher:

import zmq
import random
import sys
import time

port = "6701"
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)

NodeJS publisher:

var zmq = require('zmq')
  , sock = zmq.socket('pub');

sock.bindSync('tcp://127.0.0.1:6701');
console.log('Publisher bound to port 3000');

setInterval(function(){
  console.log('sending a multipart message envelope');
  sock.send(['kitty cats', 'meow!']);
}, 500);

The NodeJS publisher works with both the Python and the NodeJS subscriber but the Python publisher works only with the Python subscriber and not the NodeJS one.

Fixed.

Removing node_modules and reinstaling it by npm install zmq.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM