Sending/Receiving messages on the EventBus using ZMQ
Receiving messages
Components inside Openvisulizer use messages to communicate with each other. There is a lot of messages. Subscribing to all of them will impact the latency of Openvisualizer and it's also likely that you are only interested in a subset of all messages.
You can use the control plane socket to set up which kind of messages you want to receive:
#!/usr/bin/env python import zmq context = zmq.Context() socket = context.socket(zmq.REQ) socket.connect ("tcp://localhost:50001") # To subscribe socket.send_json({"sub": ["bonjour"]}) # To unsubscribe # socket.send_json({"unsub": ["bonjour"]}) # you can even do both # socket.send_json({"sub": ["Hello"], "unsub": ["bonjour"]}) message = socket.recv() print(message) # You will get the subscriptions of the PUB socket {"subscriptions":["Hello"]}
Once this is set up, you can receive messages using a snippet of code similar to this :
#!/usr/bin/env python import zmq context = zmq.Context() subscriber = context.socket(zmq.SUB) subscriber.connect("tcp://localhost:50002") subscriber.setsockopt(zmq.SUBSCRIBE, "") while True: string = subscriber.recv_json() print(string)
Sending messages
There is a REP socket waiting to receive message and inject them directly in the message bus. You can send message to the event bus by using a ZMQ REQsocket like this:
#!/usr/bin/env python import zmq import time d = {'signal': "bonjour", "sender": u"poipoi", "data": "hello world!"} context = zmq.Context() publisher = context.socket(zmq.REQ) publisher.connect("tcp://localhost:60000") count = 0 while True: count += 1 publisher.send_json(d) message = publisher.recv_json() print "message {0} ".format(count),message time.sleep(2)