0

I'm programming a broker on my Raspberry Pi with Python and I have the sub.js and pub.js programmed in JavaScript. The pub and the sub are tested and work correctly, but the broker code doesn't react.

PUSH.js

const{PUSH}= require('./config');
var zmq = require("zeromq"),
 sock = zmq.socket("push");

sock.connect("tcp://"+PUSH);
console.log("PUSH connected to port 24041 from the broker");

setInterval(function() {
  console.log("PUSH sending to broker");
  sock.send("example");//send to broker
}, 500);

SUB.js

const{sub}= require('./config');
var zmq = require("zeromq"),
  sock = zmq.socket("sub");

sock.connect("tcp://"+ sub);
console.log("sub connected to port 24042 from the broker");
sock.subscribe(""); //term what sub send

sock.on("message", function (msg) {
  //Broker received what pub send
  console.log("Broker has received: %s", msg.toString());
});

Output from working broker sending "example" Output from raspberry pi for PUSH.js and PUB.js

With the broker code I want receive and print from PUSH.js (port 41) and send back to SUB.js (port 42).

import zmq

import time

def main():

    """main method"""

    # Prepare our context and publisher
    context   = zmq.Context()
    publisher = context.socket(zmq.PUSH)
    subscriber = context.socket(zmq.SUB)
    publisher.connect("tcp://address:42")
    subscriber.connect("tcp://address:41")
    subscriber.setsockopt(zmq.SUBSCRIBE, b"example")

    while True:
        #print received contents from port 41
        [address, contents] = subscriber.recv_multipart()
        print("[%s] %s" % (address, contents))
        #send received contents from port 42
        publisher.send_multipart([address, contents])

    # We never get here but clean up anyhow
    publisher.close()
    subscriber.close()
    context.term()  
    if __name__ == "__main__":

    main()

output broker: nothing print

  • Can you confirm if your indentation in your answer reflects your actual code? It looks like the call to `main()` is within the `main()` function declaration, so please edit your post to reflect what it actually looks like since I couldn't make that assumption for you. – Mihai Chelaru Apr 15 '20 at 13:16
  • In what way is the broker supposed to react? The snippet looks to me like a repeater i.e it repeats whats received from subscriber and publishes it on publisher. I would start debugging by making sure that I am receiving the data properly (try printing). You could also just dummy some data internally to see if the publishing is working as well. Also double check your port assignments. – Jason Chia Apr 15 '20 at 13:16

1 Answers1

0

In case one has never worked with ZeroMQ,
one may here enjoy to first look at "ZeroMQ Principles in less than Five Seconds"
before diving into further details



1 ) make sure the port numbers do match -- 41 != 24041 / 42 != 24042
2 ) make sure each link has at least one .bind()-node and all others .connect() to that one address/port, so as to make both the PUB/SUB and PUSH/PULL archetypes indeed work together
3 ) make sure to follow the ZeroMQ published API & avoid incompatible setups, like trying to bond PUSH/PUSH.

user3666197
  • 1
  • 6
  • 45
  • 89