In the last post we saw a simple PubSub application using ZeroMQ. Today we are going to extend that and publish all the data we get from the ZeroMQ publishers to web clients by using WebSocket.
First problem I ran into was choosing a WebSocket server to suit my needs. I had a look at Eventlet, Gevent, txWebsocket and some others, but since what I wanted was quite simple I decided to go and write my own, in order to avoid such dependencies. Communicating both worlds (ZeroMQ and WebSocket) seemed like the big problem, so I thought the best way to solve it would be to put all sockets in a polling loop running on its own thread and avoid possible multithreading issues. This works thanks to the ZeroMQ Poller, which is able to poll ZeroMQ sockets and anything which defines a fileno() method. Awesome stuff.
Since the single threaded polling experiment worked I decided to make a small standalone project (without ZeroMQ, using select.poll
instead) with it for possible future stuff. You can find it here.
Back to our gateway, lets go and check the code:
producer:
[gist]https://gist.github.com/1046944[/gist]
As you may have guessed, its the same simple multicast producer we saw on the previous post.
client web page:
[gist]https://gist.github.com/1051836[/gist]
consumer and WebSocket server:
[gist]https://gist.github.com/1051872[/gist]
I had never used poll
nor ZeroMQ Poller before, but they were both pretty easy to get started with. In a nutshell, the server will take every piece of data it gets from the ZeroMQ socket and broadcast it to all the connected web clients through web sockets. The gateway is one way only, the input coming from the web sockets is not sent anywhere, I’ll leave that as an experiment for the reader. :–)
To test it just run the server and serve the page. You can use this to serve the page.
:wq