Search code examples
pythonnode.jszeromqpublish-subscribe

Subscribe is not working while publishing from Python


Original question is here: https://github.com/JustinTulloss/zeromq.node/issues/444

Hi,

If I subscribe from Node.js to a publisher in Python, subscriber can not receive messages. On the other hand, Node-publisher can send both python-subscriber and node-subscriber, python-publisher can send python subscriber.

Node subscriber:

// Generated by LiveScript 1.4.0
(function(){
  var zmq, sock;
  zmq = require('zmq');
  sock = zmq.socket('sub');
  sock.connect('tcp://127.0.0.1:3000');
  sock.subscribe('');
  console.log('Subscriber connected to port 3000');
  sock.on('message', function(message){
    return console.log('Received a message related to: ', 'containing message: ', message.toString());
  });
}).call(this);

Node publisher:

// Generated by LiveScript 1.4.0
(function(){
  var zmq, sock;
  zmq = require('zmq');
  sock = zmq.socket('pub');
  sock.bindSync('tcp://127.0.0.1:3000');
  console.log('Publisher bound to port 3000');
  setInterval(function(){
    console.log('Sending a multipart message envelope');
    return sock.send('TestMessage(node)!');
  }, 1500);
}).call(this);

Python publisher

import zmq
import time

context = zmq.Context()
publisher = context.socket(zmq.PUB)
publisher.bind("tcp://127.0.0.1:3000")

while True:
    time.sleep(1)
    publisher.send("TestMessage")
    print "Sent"

Python subscriber:

import zmq

context = zmq.Context()
socket = context.socket(zmq.SUB)

socket.setsockopt(zmq.SUBSCRIBE, "")
socket.connect("tcp://127.0.0.1:3000")

while True:
    string = socket.recv()
    print string

Solution

  • The problem is the mismatch of libzmq version between PyZMQ and zeromq.node:

    $ python 
    >>> import zmq
    >>> zmq.zmq_version()
    '4.0.5'
    

    and node version:

    $ node
    > require('zmq').version
    '2.2.0'
    

    Solution is:

    1. Uninstall current version of libzmq: sudo apt-get purge libzmq-dev
    2. Uninstall current zeromq.node: sudo npm uninstall zmq -g
    3. Install libzmq-4.x: sudo apt-get install libzmq3-dev

      if this step fails, you need to install libzmq-4.x from source: https://github.com/zeromq/libzmq

    4. Install zeromq.node: sudo npm install zmq