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