Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realtime options (Websockets, flash, polling) for Django?

What are some realtime "push" options for django that can install as a python package? I want to avoid having to do things like installing independent web-servers for realtime.

Essentially I am looking for something like pusher.com (cloud system) or this socket.io build for django (which has a build status:failing) for chat and other various push operations.

Ape was suggested here, but it seems it requires you to setup Ape as a server. If its not too much to ask for, are there any solutions that build right into django?

like image 387
NoviceCoding Avatar asked Apr 05 '12 19:04

NoviceCoding


2 Answers

Since the time the answer was written (2012); a lot has changed.

The preferred method now to do realtime updates of the system is using websockets; which is being formalized and proposed as a standard RFC 6455. This page on MDN has a great overview of the technology.

The other emerging technology is Server Sent Events which is a W3C draft proposal.

Projects such as swampdragon and django-socketio make integrating realtime functionality easier in your project.


There are two main components to any realtime system:

  1. A connection that remains open from the browser to a server.
  2. A server that listens on this connection and then responds.
  3. A system / standard to store and be notified of messages.

Okay so maybe three components.

Since django doesn't work in realtime any solution that offers realtime push/updates will require another server/service to accept messages and then notify listeners of pending messages.

Django would be the application that pushes messages (writes them) to this server on a channel (a queue/bucket). Listeners then subscribe to a channel to be notified of messages. Since the connection remains open; messages are retrieved in "realtime".

Django really has a minimal role in all of this. There are various implementations that provide the three components necessary for realtime notifications to work.

I really like juggernaut because it is super simple to set up, and uses node.js that doesn't require a lot in terms of server-side components. The other reason I prefer it is because it supports Adobe Flash Socket in addion to WebSocket (and others, see the link).

The api to access it also very simple - in fact, if you are already using redis (which you really should since its so easy to use), you don't need another API as you can drop messages to redis and juggernaut will read them, or you can use its Python API. A simple example from this flask snippet:

Send (write) a message to a channel:

>>> from juggernaut import Juggernaut
>>> jug = Juggernaut()
>>> jug.publish('channel', 'The message')

Listen to it:

<script type=text/javascript
  src=http://localhost:8080/application.js></script>
<script type=text/javascript>
  var jug = new Juggernaut();
  jug.subscribe('channel', function(data) {
    alert('Got message: ' + data);
  });
</script>
like image 85
Burhan Khalid Avatar answered Sep 23 '22 12:09

Burhan Khalid


Django is built to serve web pages and there is nothing out of the box to support websockets in django. The quickest/easiest option is pusher.com (I use it an really like it). You can start with something like pusher.com and if you write a quick wrapper around it, you can replace it with your own server using socket.io or any other web socket server by just changing the wrapper / interface to connect to the new server. Just make sure you write it with being able to switch out the backend at any time.

If you really want to start run your own socket server there are projects out there that will make it easy to use sockets in django:

  • django-websocket
  • django-socketio
like image 25
hazmat Avatar answered Sep 23 '22 12:09

hazmat