A ZeroMQ to WebSocket gateway, take 2

A while ago I posted a way to build a gateway between ZeroMQ and WebSocket. That mechanism required a custom WebSocket server which has to be maintained and I wasn’t really happy with it anyway so I thought I’d find another way.

Moreover, WebSocket is probably not enough. Since the WebSocket specification has changed a lot, different browsers have implemented different versions of the draft. WebSocket could even be disabled god know why, so we need a fallback mechanism. And then I discovered SocketIO. With SocketIO you can establish a persistent connection between a browser and a server in many different ways: WebSocket, flash socket, long polling, etc. This is great stuff.

If I’m not mistaken SocketIO was born for NodeJS but then was ported to some other programming languages and frameworks. The one I’m particularly interested in is TornadIO. TornadIO is a SocketIO implementation on top of Tornado. In case you have lived in a cave for the last several years ;–) you should know that Tornado is the webserver that powered FriendFeed, which was acquired by Facebook.

So, we now have a nice persistent connection between the browser (using SocketIO JavaScript library) and our server (using TornadIO), but how do we add ZeroMQ to the mix? It couldn’t be easier: ZeroMQ added a way to integrate its event loop with the one in Tornado, so there is really nothing else to be done to integrate them. :–)

Here is an example of a ZeroMQ – WebSocket (SocketIO really) gateway built as I described above. The client will connect to the server (using the web browser) through SocketIO and will just sit there waiting to get data. The server will push anything that arrives through the ZeroMQ socket to all clients connected through SocketIO and the messages will just get printed on the screen. It’s a simple example, but you get the idea ;–)

:wq

  • Shrirang

    Hi Saghul,This is pretty cool. Is SocketIO available for all the bowsers and not interfered by firewalls & proxys?I am not a Python expert but was wondering if I could get NodeJS there instead to trap the socket calls and route them to ZeroMQ in my C++ server code.Shrirang

  • Saúl Ibarra Corretgé

    Hi,SocketIO uses different mechanisms to try to work under any circumstance: it implements WebSocket, Flash Socket, HTTP long polling, etc.What you propose seems doable, NodeJS can handle the SocketIO part, but I’m not sure if there are ZeroMQ bindings for it.

  • inv1te

    Hi,thank you! Post is very nice example use tornadio and zeromq.How can I send data from the web browser to zeromq (socket.io – s.send(data), tornadio – on_message, zeromq – ?) ? Perhaps you already have a solution?

  • Saúl Ibarra Corretgé

    I didn’t implement it but it would be trivial to do it since the involved sockets are bi-directional. If you find some trouble doing it, feel free to contact me 🙂