old blog,

Simple websocket push service using Tornado

Izhar Firdaus Izhar Firdaus Follow Support Oct 02, 2011 · 1 min read
Simple websocket push service using Tornado
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.
WebSocket server side documentation/guides around tend to be very node.js oriented. However, I don't fancy rewriting stuff I already have simply to be able to make use of WebSocket. After some research, I figured out a mechanism for a service which will handle push for WebSocket, in a simpler manner which are familiar to everyone - simple POST/GET!

Example server code: (on Tornado 11.0)

import tornado.ioloop
import tornado.web
from tornado import websocket

GLOBALS={
'sockets': []
}

class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")

class ClientSocket(websocket.WebSocketHandler):
def open(self):
GLOBALS['sockets'].append(self)
print "WebSocket opened"

def on_close(self):
print "WebSocket closed"
GLOBALS['sockets'].remove(self)

class Announcer(tornado.web.RequestHandler):
def get(self, *args, **kwargs):
data = self.get_argument('data')
for socket in GLOBALS['sockets']:
socket.write_message(data)
self.write('Posted')

application = tornado.web.Application([
(r"/", MainHandler),
(r"/socket", ClientSocket),
(r"/push", Announcer),
])

if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()

Example Client Javascript (jQuery)

$(document).ready(function () {
var ws = new WebSocket("ws://localhost:8888/socket");
ws.onmessage = function(event) {
$('body').append('<div>' + event.data + '</div>');
}
$('body').append('<div> Start! </div>');
});

How to try

Start the Tornado server, load http://localhost:8888/push?data=somedatahere. This will send 'somedatahere' to the websockets and notify them all with the string.

Probably this can evolve to a nicer service system. But I'll hack on it more later
Written by Izhar Firdaus Follow Support
I'm a system architect, data engineer and developer advocate with passion in Free / Open Source software, entrepreneurship, community building, education and martial art. I take enjoyment in bridging and bringing together different FOSS technologies to help businesses and organizations utilize IT infrastructure to aid and optimize their business and organizational process.

Introducing collective.trajectory : URL Routing, in Plone!!

When I was working with Martijn Faassen, I was introduced to traject, which is a library written by Martijn for URL routing. It is av...

In old blog, Sep 26, 2011

« Previous Post

Radiate: A simple push server for SocketIO (alpha)

Based on my previous post on WebSocket server using Tornado, I've hacked the code more to be a SocketIO server instead of just WebSoc...

In old blog, Oct 03, 2011

Next Post »