Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSocket + Django python WebService

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?

like image 308
Gero Avatar asked Feb 28 '12 21:02

Gero


1 Answers

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

like image 169
christophe31 Avatar answered Sep 30 '22 02:09

christophe31