A quick chat WebSockets/AMQP client
In my previous article I described how to plug WebSockets into AMQP using Tornado and pika. As a follow-up, I’ll show you how this can be used to write the simplest chat client.
Every time, the user enters a message and submits it too our WebSockets endpoint which, in return, will forward any messages back to the client. These will be appended to the textarea.
Let’s see how we had to change the Tornado application to support that handler as well as the serving of jQuery as a static resource (you need the jQuery toolkit in the same directory as the Python module).
if __name__ == '__main__': application = tornado.web.Application([ (r"/", MainHandler), (r"/ws", WebSocket2AMQP), ], static_path=".", bus=bus) http_server = tornado.httpserver.HTTPServer(application) http_server.listen(8888) bus.subscribe("main", poll) WS2AMQPPlugin(bus).subscribe() bus.start() bus.block()
The code is here.
Once the server is running, open two browser windows and access http://localhost:8888/. You should be able to type messages in one and see them appears in both windows.