Hi,
This is the third feature proposal as part of my general drive for
getting Django to work better for javascript heavy sites.
Support Server-Sent Events
--------------------------
If you want a snappy user experience, AJAX polling isn't enough. There
are two major ways to get out of the request-response cycle. Namely:
websockets and server-sent events.
Websockets are complicated beasts, and getting cross-browser support for
them requires implementing several protocol versions. They are also
bidirectional, meaning you can send stuff to the server with them.
Server-Sent Events use normal HTTP, and are natively supported
everywhere except Internet Explorer. Since they don't use a custom
protocol there are several polyfills that enable IE support too,
bringing browser compliance close to 100%. SSE only supports sending
data from server to client, not the other way around.
The ease of implementation for server-sent events makes it a much better
Django fit in my opinion. Also, I don't think the need for client ->
server requests is that big, you can easily solve that with AJAX calls
instead.
More reading on how it works:
http://www.html5rocks.com/en/tutorials/eventsource/basics/
---
Just like websockets, server-sent events rely on persistent connections
to each connected client. This means that we need a separate process to
make this work properly. I understand this is a controversial
suggestion, but it allows the rest of Django to continue working like
before, and still offer async support to users. It's the best of two worlds.
So how would you implement such a process in a performant way? With
Python 3's asyncio package (for Python 2 support there's trollius:
https://pypi.python.org/pypi/trollius). This means no external
dependencies (except for old pythons).
You would start the process separately, add a script tag to your page,
and all clients that connected to the page would be connected to the
process. Now Django could push messages through that process as part of
the request-response cycle, and all clients would be notified. Other
processes (a cron job?) could do the same.
Simply stated: we would finally get async support in Django, without
having to rewrite large parts of Django. Also, we need no persistent
message storage. Connected clients get the messages, then it's discarded.
There is existing work on this that could be a starting point for a more
Django:y API: https://github.com/brutasse/asyncio-sse
---
I think server-sent events would be a great reason to choose Django over
other frameworks. It would finally answer the request for "async"
features in Django, with a system that's as easy to setup (just start
the process) as to implement.
Would anyone we willing to work with me on this? Do you think it makes
sense to put this in Django? I don't see any need for this code to
change often, and it fits well with the "building apps quickly and with
less code" mantra.
Thoughts? Ideas?
(And yes, this idea would work great together with the js template
rendering. Pushing database updates from the server to the client, and
they instantly updating the template accordingly)
Regards,
Emil Stenström
Twitter: @EmilStenstrom
--
You received this message because you are subscribed to the Google Groups "Django
developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-developers/5569EACC.4040400%40kth.se.
For more options, visit https://groups.google.com/d/optout.