Implementing the real-time web with django - Kristian Øllegaard

Tags: django, djangocon

Why do we want to make real time websites? Better user experience. More options on the front end. It makes the web feel more like native apps. You can show live data! Collaboration is much easier.

When you want to make it with django, you need to find the right tool. Django itself as such is not really suited. There are some criteria:

  • Some good socket-or-whatever-alternative-you-need implementation.
  • You need good browser support (also old IE versions).
  • It should be usable from python, it doesn’t need to be written in python per se.
  • You should not have to do a full-scale rebuild of an existing front-end.

What you want is node.js and socket.io. Node.js is build on chrome’s javascript runtime and it uses event-driven non-blocking IO. Socket.io uses one interface for all communication channels (whether web sockets or something else.

Why not in python? Well, there’s already a good nice big community around node.js/socket.io. And it can actually be used from python without too much trouble. Because most people know at least some javascript, node.js should not be too much of a hurdle.

You need to communicate between python and node.js. Redis works fine for cross-language communication. Redis supports many datatypes, so that’s fine.

The basic setup: Django or celery will publish items to redis, node.js will subscribe to redis and get the content into the browser. Example usage from python looks like this:

import redis
import json
redis_subscribe = redis.StrictRedis()
redis_subscribe.publish('socketio_news',
                        json.dumps("Hey, how are you?"))

You need to host socket.io, but nginx does not support websockets. The recommended way is to expose the node.js server directly (which is not a problem because node.js scales).

Can I use this today? Well, yes, but please don’t. Authentication is a problem still. Unless you do many tricks, you can basically only do it for very public data. There’s no authentication between django and node.js. Perhaps store your sessions in redis and check them on connection?

Nice thing: communicating between django and socket.io via redis like this should in theory work for many other usecases. Like letting python talk to ruby or so.

Stuw
blog comments powered by Disqus
 
vanrees.org logo

About me

My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.

Weblog feeds

Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):