Real-timify Django with SockJS

06 September 2012   4 comments   Django, Tornado, Javascript

Mind That Age!

This blog post is 5 years old! Most likely, its content is outdated. Especially if it's technical.

Powered by Fusion×

In yesterdays DjangoCon BDFL Keynote Adrian Holovaty called out that Django needs a Real-Time story. Well, here's a response to that: django-sockjs-tornado

Immediately after the keynote I went and found a comfortable chair and wrote this app. It's basically a django app that allows you to run a socketserver with manage.py like this:

python manage.py socketserver

Chat Demo screenshot
Now, you can use all of SockJS to write some really flashy socket apps. In Django! Using Django models and stuff. The example included shows how to write a really simple chat application using Django models. check out the whole demo here

If you're curious about SockJS read the README and here's one of many good threads about the difference between SockJS and socket.io.

The reason I could write this app so quickly was because I have already written a production app using sockjs-tornado so the concepts were familiar. However, this app has (at the time of writing) not been used in any production. So mind you it might still need some more love before you show your mom your django app with WebSockets.

Comments

Ionel Cristian Mărieș
Wouldn't django model queries block the tornado ioloop ? This would be very bad for apps with busy db servers ...
Peter Bengtsson
It certainly would. But only if they're slow.

If you need to connect slow db queries to websockets, then django isn't the tool at all. If that's the case you can let your slow db queries write the results to something like redis and then connect your websockets to redis instead.

Or you bite the bullet and abandon Django in favor of something purely in tornado that handles async database queries like Momoko.
Preston Holmes
Peter,

Certainly lots of cool stuff being explored in the realtime space.

My own experiment which I gave as a lightning talk on Wed morning is posted here:

https://github.com/ptone/django-live

It does a few additional things including the management of a live collection of objects. I think we need a bunch of these things to be built to explore what the common patterns are, and what if anything Django can provide to ease the stuff that gets repeated each time.

-Preston
Peter Bengtsson
Hi Preston,
Unfortunately I didn't make it to your open space about Realtime. I worked a large part of my time at djangocon and thus missed a lot of interesting events.

I see you're using something psycogreen which I'm not familiar. I'll keep that in mind in the future.

I have now got a couple of launched sites with sockets and I've never used Django for any of them. However, my django-sockjs-tornado is kinda silly because you probably wouldn't want to mess around with the ORM in a realtime environment anyway. If I were to write something decent I'd let django signals write to a pubsub redis which the live sockjs connections could pick up.

Keep up the good work!
Thank you for posting a comment

Your email will never ever be published


Related posts

Previous:
django-mongokit now compatible with Django 1.4 11 August 2012
Next:
Introducing: League of Friends on Around The World 15 September 2012
Related by Keyword:
Using Fanout.io in Django 13 December 2016
Are WebSockets faster than AJAX? ...with latency in mind? 22 April 2012
Related by Text:
Cryptic errors when using django-nose 07 December 2011
Introducing django-spellcorrector 28 May 2009
Goodies from tornado-utils - part 3: send_mail 24 September 2011
Goodies from tornado-utils - part 1: TestClient 20 September 2011
Goodies from tornado-utils - part 2: tornado_static 22 September 2011