WebSocket server with Python Tornado
This tutorial will teach you how to build a WebSocket server with the Python Tornado library.
In my previous tutorial, HTML5 and JavaScript WebSockets using MVC, you saw how to write a WebSocket client.  But you probably want to use WebSockets to do something useful on your own website, rather than connecting to someone else's server.
Websockets are a feature of HTML5 that allow for asynchronous communication between the web server and the client. Â
The existing web development paradigm looks like this:
The client requests content, which is retrieved from the server. Â If the client wants updated information, they have to make a new request.
With WebSockets the client can hold a connection to the server, receiving updated information when it becomes relevant.
This creates an event-driven model of the web, rather than the previous polling model.
Why Tornado? Â Because it's the only one that worked when I tried to install it! Â I'm a big fan of things that work as advertised. Â Also Python has excellent tutorials and lots of developers building web applications on it. Â
There are other WebSocket server environments in other languages such as C++ and Java. Â Python is a popular language for web site development, unlike C++ and Java, meaning it's possible to write your web site and WebSocket server in the same language, using the same libraries and the same framework. Â
Also, it's very simple to set up, meaning you spend your time working on your website functionality rather than your frameworks.
With that in mind, you should install the Tornado library:
The infrastructure behind WebSockets are complex, but the interface for developers to connect is simple. Â WebSockets clients support the following interfaces:
onopen() # sent when the server makes a connection onmessage(String) # sent from server when the server receives a message from the client onerror(String) # sent when the server encounters an error onclose() # sent when the client or server closes the connection open() # create a connection to the server send() # send a message to the server close() # close the connection with the server
At the server end, Tornado supports the following interfaces:
open(WebSocketHandler) # sent when there is a new connection on_message(WebSocketHandler, String) # server receives a message from the client on_close() # client closed the connection write_message(WebSocketHandler, binary=false) # send a message to the client close() # close the connection
All this is implemented in the Tornado library. All you have to do is import the tornado library and create a WebSocketHandler to handle the WebSocket server's messages.
#!/usr/bin/python # import the libraries import tornado.web import tornado.websocket import tornado.ioloop # This is our WebSocketHandler - it handles the messages # from the tornado server class WebSocketHandler(tornado.websocket.WebSocketHandler): # the client connected def open(self): print "New client connected" self.write_message("You are connected") # the client sent the message def on_message(self, message): self.write_message(message) # client disconnected def on_close(self): print "Client disconnected" # start a new WebSocket Application # use "/" as the root, and the # WebSocketHandler as our handler application = tornado.web.Application([ (r"/", WebSocketHandler), ]) # start the tornado server on port 8888 if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.instance().start()
If you run it from using Python from your command line, you can use your WebSocket client from the last tutorial to connect and try sending messages.
Connect and you should see something like this:
Congratulations on your new WebSocket server! Now you can begin using WebSockets in your Internet application!
The source code for this project is available on GitHub:
https://github.com/backupbrain/tornado-websocket-echo-server
Tornado WebSocket Documentation
http://www.tornadoweb.org/en/stable/websocket.html
Explains how tornado works and how to use it.
Websockets Server
http://mbed.org/cookbook/Websockets-Server
Explains how to build a WebServer client in HTML5 and server in tornado. I like this tutorial because it demonstrates how how to initialize a web server in the same code.
Getting Started with HTML5 WebSockets
http://www.linuxforu.com/2012/04/getting-started-with-html5-websockets/
Explains how to build a WebServer client in HTML5 and server in tornado.