Plugging AMQP and WebSockets

In my last article, I discussed the way the WSPBus could help your management of Python processes. This time, I’ll show how the bus can help plugging in heterogeneous frameworks and manage them properly too.

The following example will plug the WebSockets and AMQP together in order to channel data in and out of a WebSockets channel into AMQP exchanges and queues. For this, we’ll be using the Tornado web framework to handle the WebSockets side and pika for the AMQP one.

pika uses the Python built-in asyncore module to perform the non-blocking socket operations whilst Tornado comes with its own main loop on top of select or poll. Since Tornado doesn’t offer a single function call to iterate once, we’ll be directly using their main loop to block the process and therefore won’t be using the bus’ own block method.

Let’s see how the bus looks like

 class MyBus(wspbus.Bus):
    def __init__(self, name=""):
        self.subscribe("log", self._log)
        self.ioloop = tornado.ioloop.IOLoop.instance()
    def call_main(self):
    def block(self):
        ioloop = tornado.ioloop.IOLoop.instance()
        except KeyboardInterrupt:
    def exit(self):
    def open_logger(self, name=""):
        logger = logging.getLogger(name)
        h = logging.StreamHandler(sys.stdout)
        h.setFormatter(logging.Formatter("[%(asctime)s] %(name)s - %(levelname)s - %(message)s"))
        self.logger = logger
    def close_logger(self):
        for handler in self.logger.handlers:
    def _log(self, msg="", level=logging.INFO):
        self.logger.log(level, msg)

Next we create a plugin that will subscribe to the bus and which will be in charge for the AMQP communication.

class WS2AMQPPlugin(plugins.SimplePlugin):
    def __init__(self, bus):
        plugins.SimplePlugin.__init__(self, bus)
        self.conn = pika.AsyncoreConnection(pika.ConnectionParameters('localhost')) ="X", type="direct", durable=False)"Q", durable=False, exclusive=False)"Q", exchange="X", routing_key="")
, queue="Q")
        self.bus.subscribe("ws2amqp", self.ws2amqp)
        self.bus.subscribe("stop", self.cleanup)
    def cleanup(self):
        self.bus.unsubscribe("ws2amqp", self.ws2amqp)
        self.bus.unsubscribe("stop", self.cleanup)"Q")"X")
    def amqp2ws(self, ch, method, header, body):
        self.bus.publish("amqp2ws", body)
    def ws2amqp(self, message):
        self.bus.log("Publishing to AMQP: %s" % message)"X", routing_key="", body=message)

The interesting bits are the amqp2ws and ws2amqp methods. The former is called anytime the AMQP broker pushes data to our AMQP consumer, we then use the bus to publish the message to any interested subscribers. The latter publishes to AMQP messages that come from the WebSockets channel.

Next let’s see the Tornado WebSockets handler.

class WebSocket2AMQP(websocket.WebSocketHandler):
    def __init__(self, *args, **kwargs):
        websocket.WebSocketHandler.__init__(self, *args, **kwargs)
        self.settings['bus'].subscribe("amqp2ws", self.push_message)
    def open(self):
    def on_message(self, message):
        self.settings['bus'].publish("ws2amqp", message)
    def on_connection_close(self):
        self.settings['bus'].unsubscribe("amqp2ws", self.push_message)
    def push_message(self, message):

The on_message method is called whenever data is received from the client, the push_message is used to push data to the client.

Finally, we setup the plug everything together:

if __name__ == '__main__':
    application = tornado.web.Application([
        (r"/ws", WebSocket2AMQP),
        ], bus=bus)
    http_server = tornado.httpserver.HTTPServer(application)
    bus.subscribe("main", poll)

Notice the fact we subscribe the asyncore poll function to the main channel of the bus so that pika works properly as if we had called asyncore.loop()

The code can be found here.

6 thoughts on “Plugging AMQP and WebSockets

  1. Thomas Herve

    I don’t know Tornado well, but doesn’t call_main run in the main thread? If so, the time.sleep call would block any requests, wouldn’t it?

  2. Andrew B.

    Sylvain, there is nothing special about it. Cyclone is a Twisted-based web framework, so txamqp will work as shown in its included example code.

  3. Sylvain Hellegouarch Post author

    @Thomas: Yes you are right. But this is part of using an async framework, you have to be careful about long operations. Now, in this case, it was meant mostly as an example and I’m using the time.sleep to prevent the process from consuming my CPU like crazy.

Comments are closed.