Building single-page web-applications with Django, Twisted and TxWebsocket - Jeroen van Veen

Tags: django, pun

(Presentation at the Dutch Django/web meeting)

Websockets give you a persistent TCP connection between a browser and your server, which is handy if you want to exchange data between the two of them continuously.

How it works? The client makes an initial handshake request which is then used to set up a real connection. The protocol of the handshake is upgraded from time to time, so you have to make sure your service stays up to date with the latest browsers.

The good thing about a socket connection is that there’s very low latency: you don’t lose time with setting up an http connection all the time and you don’t have headers. And… serializing json is easy. But you can also transfer binary data.

For Python support for websockets look at TxWebsocket (based on twisted, Jeroen prefers this). There is also “Autobahn WS”, which is commercial. And there’s “gevent-websocket”. All browsers support it, basically, but for IE you need the upcoming IE10. There’s a Flash fallback, though, if you need it.

With websockets you make single page websites. You don’t need to leave the page, becuase you use websockets to grab and send data to the webserver. Most URLS in your site will point to one single page behind the scenes. You can use javascript to modify the URL in the browser so that you retain bookmarkability. The HTML5 history API helps here. And you really should want to use good URLs in your web application, just like Django helps you make.

A problem in javascript is that you can’t have “named groups” in your javascript URL regexes like you can in Django. For that, xregexp helps.

He showed a demo of two browser windows next to each other where you could see the changes made in the one in the other.

The django part of the system is django ws. It uses requirejs to manage the javascript libraries used in the browser. There are various modules: core, misc, blog, wiki.

Some challenges: find a useable pluggable reference CMS. Cleanup the design and architecture. Proper documentation! And they want to build a community around it. Another big challenge: google can’t open the socket connection and download the content: now it only grabs the empty startpage.

 
vanrees.org logo

Reinout van Rees

My name is Reinout van Rees and I program in Python, I live in the Netherlands, I cycle recumbent bikes and I have a model railway.

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):