(One of the summaries of a talk at the 2015 Djangocon EU conference).
Aaron Bassett says there are two kinds of polling you can do upon http: lots of small requests all the time, hoping that there’s something new on the server. And “long polling”, where a connection is opened and kept open until the server has something to send back. Once received, a new http request is send out.
But… in the core, that’s a hack. http isn’t designed for that.
Now there’s websockets. Persistent connections where you can send messages back and forth between server and browser.
There is a django package to make real time web easy: swamp dragon. Swamp dragon consists of:
Redis. Basically a very quick, persistent kind of memcache. Supports pubsub.
Tornado. A python webserver. Non-blocking IO. Lots of connections are no problem.
Django.
Swamp dragon takes care of all the real time stuff for you. He made a sample TODO django app. Some notes:
There’s a swamp dragon model mixin that makes sure any changes to the models are send to Redis.
You have swamp dragon serializers that tell which data you want to extract from the models. And you can add extra information, of course.
The serializers look a lot like django rest framework’s serializers. They’re working to support the DRF serializers. This helps with the code duplication you might have to have.
The URLs are handled via ModelRouters.
They used angularjs to talk to the websocket and keep the list of TODOs up to date.
He showed a demo. It looked to work really fast and responsive.
If you already have a django site, perhaps you’re not really interested in adding redis and so to the mix. You could look at software-as-a-service providers like firebase or pubnub or pusher (a conference sponsor).
They now use pusher. It has a python library to talk to it. So they replaced redis + tornado with pusher. Pusher also has an angularjs library, so that helped getting it running quickly.
His demo code is at https://github.com/aaronbassett/djangocon-pusher
He afterwards showed the third example to get notifications in django if someone else is editing the same object, including javascript integration: https://pypi.python.org/pypi/django-pusherable
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.
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):