59
HTTP is inherently a âpullâ protocolâi.e., a client pulls data from a server, waits around for a while and then pulls more data later. Thereâs actually no strictly HTTP way to âpushâ data to a client from a server.
You have basically three options when you need to âpushâ to a client.
(1) Do pollingâuse Ajax/javascript to poll the server every X amount of time. The smaller the X the more it âfeels likeâ a push, but also the more overhead your server experiences having to constantly respond to these requests.
(2) Use websockets. Part of the HTML5 spec is something called websockets. Websockets allows a browser to open up a persistent connection to a server. Once this connetion has been opened data can be pushed back and forth from client to server and server to client just like with more traditional TCP sockets. The trouble with websockets (last I heard) was that they can still be a bit temperamental between browsers, and of course wont work at all in older browsers.
(3) Use Flash with a Javascript interface. Flash has the capability of setting up persistent TCP connections, which can be used to push/pull data just like with a ânormalâ TCP connection. (See this SO question as well: HTTP push examples in Flex)
If you were starting this project from scratch I would recommend you write your backend in Node.js with Socket.io. Socket.io is âsocket-likeâ framework that you can program to and then the Javascript client (which runs in your webbrowser) intelligently determines the best âpersistent connectionâ to useâfirst it tries to use Websockets, then Flash, then long polling of various types.
But since youâve said you want to use Python/Django then you should check out Django-Websocketsâa framework for using websockets with Django. But be sure to read the Disclaimer the author puts on the page, there are some significant difficulties/limitations associated with using it, primarily because Django wasnât designed with websockets in mind.
I think your best bet will end up being using Websockets with an intelligent fallback to Ajax Polling when the userâs browser doesnât support it.
7
If ever you use nginx, which is a good choice :), you may use the push module http://pushmodule.slact.net/, I found it rather easy to use.
You have one URL to publish messages on a channel (that can be done easily in python, with httplib for example), and one URL to pull messages from a channel (and a channel may be used by more than one user). See also http://blog.jamieisaacs.com/2010/08/27/comet-with-nginx-and-jquery/ for a jquery integration.
- [Django]-Invalid command WSGIDaemonProcess Deploy Django application on CentOS 6.7
- [Django]-How to use dynamic foreignkey in Django?
- [Django]-Filter on prefetch_related in Django