News for hackers. Stuff that matters.
Re:The easiest way to add WebSockets to Django by falcolas on Monday January 27, 2014 @11:01AM (#7130989)
No, it's really not that easy, sorry.

Looking at the code, you're not actually doing anything with websockets here. There's no handshake parsing, there's no open socket object to pass data back and forth on, there's nothing actually websockety about this.

Instead, you seem to have a simple ajax endpoint.

Or am I missing something?

[EDIT] Clarifying why I said it's not that easy, since that seems snarky as I read it again. To properly do websockets in Django, you have to write your own runserver, which allows processes up the stack to capture the raw socket, perform the websocket handshake methods, and do proper parsing of the incoming data to break the messages on the sentinel values.

I recently tried to do this, and the one package that did this wouldn't work with Django 1.6, and we would have to give up our flup runfcgi, which I wasn't ready to do.

Re:The easiest way to add WebSockets to Django by gizmo on Monday January 27, 2014 @11:07AM (#7131027)
I only looked at it for a second or so, but it looks like it's a simple json proxy. You put a webserver with persistent connections on one end (Tornado in the example code) and a regular django environment side by side. The web socket requests are translated in to GET/PUT/POST requests, are pushed to django, django responds and the response is pushed to the client.

So from django's point of view there are stateless JSON requests and responses, so django's happy. The python proxy code is a lightweight wrapper that doesn't have to understand any business logic, so that's pretty straightforward as well.

Re:The easiest way to add WebSockets to Django by falcolas on Monday January 27, 2014 @11:21AM (#7131127)
I see where I was confused; I looked at the actual code itself, not the example where tornado was living and being used.

What are the advantages of this over normal AJAX requests, given that it's all using the single request, single response mode of operation (and you can compress/cache AJAX requests/responses, which websockets doesn't support yet).

Re:The easiest way to add WebSockets to Django by mcantelon on Monday January 27, 2014 @12:20PM (#7131600)
Seems like advantage is skipping network roundtrip to/from client browser. But still seems expensive in terms of cycles.
Re:The easiest way to add WebSockets to Django by nilsbunger on Monday January 27, 2014 @12:19PM (#7131593)
This could be useful. A few questions:

1) can you describe the tornado part of the system? The blog post isn't really clear on that.

2) how do you associate websocket sessions with browser sessions?

3) how do you handle server-to-client notifications? ie. a celery task completes and you want to notify the client.

4) Is XSS a concern?

Re:The easiest way to add WebSockets to Django by vivekprakash on Monday January 27, 2014 @10:21AM (#7130704)
This question is more related to websockets. I have seen that they use different protocol and doesn't work behind proxies or firewalls most of the time. How do you handle this?
Re:The easiest way to add WebSockets to Django by falcolas on Monday January 27, 2014 @11:25AM (#7131160)
Using websockets relies on a proxy that doesn't pass just the headers/body, but instead sends all packets in both directions. Nginx proxies do this, Apache requires a special proxy module.

Firewalls don't typically care, since it's effectively a long lived HTTP request, unless it digs into all of the packets and filters there.

Re:The easiest way to add WebSockets to Django by swah on Tuesday January 28, 2014 @09:16AM (#7137131)
IIRC SSL is recommended for Websockets.
Re:The easiest way to add WebSockets to Django by jpdlla on Monday January 27, 2014 @10:05AM (#7130599)
Author here. Let me know if you have any questions or feedback.
Re:The easiest way to add WebSockets to Django by fredley on Monday January 27, 2014 @10:45AM (#7130867)
Just this last week I started working on a new project with django-socketio, and I'm pretty happy with it so far. Why is django-websocket-request better?
Re:The easiest way to add WebSockets to Django by jpdlla on Monday January 27, 2014 @10:59AM (#7130965)
I did look into django-socketio when doing research, but never actually used it. django-websocket-request is way more simple, because it does way less and doesn't cover as much as django-socketio. For example, django-socketio is bound to only using socket.io, but it also includes a whole channel subscription and broadcast system so you can send messages from the server to specific channels. django-websocket-request only enables you to add a WebSocket layer over your already existing REST API / Views. I'll eventually have to look into doing things that django-socketio does.
Re:The easiest way to add WebSockets to Django by mcantelon on Monday January 27, 2014 @01:49PM (#7132247)
Does django-websocket-request, like django-socketio , support fallback to other transport methods if websocket isn't available in a browser?
Re:The easiest way to add WebSockets to Django by jpdlla on Monday January 27, 2014 @01:55PM (#7132295)
django-websocket-request doesn't really know anything about websockets specifically. The example I provided uses sockjs which provides websocket emulation and does upgrades to other methods if supported.
Re:The easiest way to add WebSockets to Django by mcantelon on Monday January 27, 2014 @02:53PM (#7132714)
Ah, nice.
Re:The easiest way to add WebSockets to Django by falcolas on Monday January 27, 2014 @11:38AM (#7131255)
Does SockJSRouter do any magic to ensure that wrapped handlers won't block the Tornado event loop?
Re:The easiest way to add WebSockets to Django by jpdlla on Monday January 27, 2014 @11:49AM (#7131336)
Not 100% sure but I would hope so. https://github.com/mrjoes/sockjs-tornado