Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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

Solution 2:

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.

Solution 3:

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.

Post a Comment for "Websocket + Django Python Webservice"