13

I was wondering how to create a django webservice (responds with XML) with websockets. I have already a django webservice which accepts xml requests, parse those requests, makes a database query, creates a response xml and send that xml back to the requester/browser. Just a normal HTTP XML request, where the response is shown as xml within the browser.

But how would i now create a websocket django webservice? Lets say i would like to send a xml response to the requester/browser with the latest data from database whenever a new magical event occurs.

I have read a lot of posts and blogs but it was kinda all too general. Can i solve this only with django + apache or do i need something else next to django and another server only to handle websockets?

I am right now using django 1.3, Apache + wsgi but i would be ready to switch any configuration that would work.

Update:

There are many possible websockets out there, http://pypi.python.org/pypi?:action=search&term=websocket&submit=search but which one could be used in my case?

Gero
  • 12,993
  • 25
  • 65
  • 106

3 Answers3

9

Sorry but django handles async requests very very poorly as it is wsgi. You'll be limited by your number of parallel instance if you have to handle real users. The best solution is to use tornado or node.js.

Tornado handles websocket and long polling brilliantly. Here is my wrapper to allow getting user and sessions from a parallel tornado thread:

https://gist.github.com/1939836

It's adapted from a more complex source, I didn't tested this gist, it's long polling but tornado handlse WebSocket as well.

http://www.tornadoweb.org/documentation/websocket.html

update:

Avoid django-websocket for production use. Even the main developer recommends against it.

I recommend Tornado because it's an awesome technology which is damningly faster/lighter than django. It may be useful for some simple cases. You'll need to configure apache/nginx anyway so, at least get "faster web pages" feature available.

Django-Desktop-Notification focuses on chrome browser and require node.js.

update (01/2016):

Mozilla gave money to django in late 2015 to solve this particular issue, the current most promizing implementation made by a django core dev is this one:

https://github.com/andrewgodwin/channels

It will probably be part of django 1.11 or 2.0

christophe31
  • 6,359
  • 4
  • 34
  • 46
  • thx for your answer. I have to avoid at any cost **polling**. Client must not ask Server for new updates. I will updated my first post with a list of "possible existing implementations" but i am not sure which one of them could be used for my case. – Gero Feb 29 '12 at 11:27
  • long polling is true RealTime, you just send answer when needed. But you have all you need to make communication between tornado async server and django. Avoid DB access from tornado if you use django orm, it's made of blocking calls. – christophe31 Feb 29 '12 at 11:33
  • I have just read the rfc to long polling, but i will have to go with WebSockets. Need one of the many implementations, which is capeable of working with django – Gero Feb 29 '12 at 13:30
  • the tornado gist I showed you allow you to use django request object and libraries from your tornado server. – christophe31 Feb 29 '12 at 14:19
  • Thank you Ross, as I'm french, I appreciate grammar correction, I know I need it. But I can't up-vote it ;-) – christophe31 May 05 '14 at 14:46
7

Although it's a bit complicated to setup (but probably the way to go), you could use gunicorn + gevent + socket.io .

I used this article to guide my way through it.

You might also look at server sent events (the article mentioned above looks at that too). If they suit your needs, it would be a bit easier to set up - since you don't have to set up socket.io and you don't need a client library. One catch though - SSE are not supported in IE.

Vlad A. Ionescu
  • 2,638
  • 1
  • 16
  • 19
0

Yeah, django is not all that great when it comes to asynchronous stuffs. My advice for you would be to use twisted as it has a lot of websocket libraries. If you really need to use django..you can make django act just as a pass through, for all the api stuff you build using twisted.

Milind
  • 560
  • 5
  • 5