25

I need to build a push system in django, basicly its function is to push messages from server to browser.

As nodejs cannot be used, i prefer websocket or orbited, but i've no idea how to implement any of these two in django. pls recommend a method for me, this will help me a lot, thx.

Stan666
  • 424
  • 2
  • 5
  • 12

7 Answers7

14

One option would be to use a WebSocket server running parallel to your Django server which has a REST/Push API, and then do pushes from Django by simply HTTP/POSTing to the WebSocket server, which in turn delivers the messages to all connected WebSocket clients.

That way, you don't need any structural/technical changes to your existing Django app, nevertheless have a scalable, modern WebSocket based push feature.

For a hosted service providing above, I'd have a look at http://pusher.com.

If you prefer running your own, I'd have a look at http://autobahn.ws, which is deployed as a ready-to-run virtual appliance (VMware, VirtualBox, Amazon EC2). You can find working examples of REST/API here https://github.com/tavendo/AutobahnPushPython

Disclaimer: I am author of Autobahn Open-source, and work for Tavendo, which offers Autobahn.ws (the commercial offering based on Autobahn OSS).

oberstet
  • 21,353
  • 10
  • 64
  • 97
11

Django isn't good at "pushing" things to the client. If django is your only option, the only way to mimic a push is via long-polling. But this is not efficient. Instead, websockets are hot. Tornado and twisted can help you here. There is also a more complete answer to your question here.

Community
  • 1
  • 1
hymloth
  • 6,869
  • 5
  • 36
  • 47
  • My whole site is based on django and Nginx, so I guess its not an efficient way to use Tornado or twisted right? – Stan666 Jun 11 '12 at 03:38
  • Why not efficient? Does it depend on your hardware? With little effort you can have a parallel tornado process that handles some websockets.. – hymloth Jun 11 '12 at 07:41
  • Sorry i am not quite familiar with tornado, do u mean a tornado server parallel to django? My first thought was to do this inside django, cause there's django-websocket which is not too bad. – Stan666 Jun 11 '12 at 07:57
  • Yes, I mean a tornado server parallel to django. Django-websocket has some caveats, which are stated in its documentation. If you invest a little time, you'll have a simple tornado websocket server up and running. This is quite easy, see here to feel the simplicity :) http://blog.kagesenshi.org/2011/10/simple-websocket-push-server-using.html. Enjoy!! – hymloth Jun 11 '12 at 08:12
  • This problem really bugs me for a long time, now i guess im close to the answer :) Thx – Stan666 Jun 11 '12 at 09:16
  • Long-polling is not inefficient (compared to polling). And Django can't do it (or websockets) directly. – remram Mar 12 '15 at 17:04
  • As @potar pointed in another answer, [SwampDragon](http://swampdragon.net) is a good Django alternative using Redis. – J punto Marcos Apr 08 '16 at 07:14
9

Another implementation of websockets in Django: https://github.com/jonashagstedt/swampdragon

From docs: SwampDragon makes use of Djangos wonderful ORM, Tornados excellent websocket support (with fallback. Tested in IE7), and Redis blazing speed.

potar
  • 468
  • 6
  • 6
9

As of 2016, there is Django Channels, which brings websockets to Django (1.8 and higher).

It is also part of the official Django project.

Community
  • 1
  • 1
Niko Föhr
  • 28,336
  • 10
  • 93
  • 96
  • Found also a full tutorial for Django + Channels + React: https://codyparker.com/django-channels-with-react/ – Niko Föhr Jun 20 '17 at 17:14
6

There is a Django module, which supports websockets out of the box using Redis as message queue. Have a look here: https://github.com/jrief/django-websocket-redis

Disclaimer: I am the author of that library.

jrief
  • 1,516
  • 13
  • 9
  • Can you recommend a minimal example? – Rubber Duck Feb 23 '15 at 17:22
  • @RubberDuck... Check the repository demo. It's pretty good. You can check your installation: http://django-websocket-redis.readthedocs.org/en/latest/installation.html#check-your-installation Here is the live demo: http://websocket.aws.awesto.com/chat/ – nicorellius Jun 18 '15 at 15:53
5

As @hymloth suggests, a better way is to use tornado or node.js.

But, if you have to use Django, I've wrote an article about implementing WebSocket with it that you might find useful.

http://curella.org/blog/2012/jul/17/django-push-using-server-sent-events-and-websocket/

fcurella
  • 2,461
  • 2
  • 19
  • 7
1

Django omnibus

Uses tornado internally and has python 3 support.

Ryu_hayabusa
  • 3,666
  • 2
  • 29
  • 32