6

I am developing a RESTFUL webservice using Django. On some occassion, we need to push the server object to the connected client without client polling. We decided to use django-websocket 0.3.0.

I am writing the test cases and tried to connect to the server using nodejs ws client module

My View Function in Django is following:

from django.http import HttpResponse
from django_websocket import accept_websocket, require_websocket
from django.views.decorators.csrf import csrf_exempt 
import json, sys, os, time, datetime

@csrf_exempt
@accept_websocket
def home(request) :
    if not request.is_websocket():
        return HttpResponse('new message')
    else:
        for message in request.websocket:
            message = modify_message(message)
            request.websocket.send(message)
            request.websocket.close()

My Client Side code in js is like this:-

//Normal Get
var request = require('request');
request('http://127.0.0.1:8000',function(err,resp,flag){
    console.log(resp.body);
});

//Opening a websocket
var WebSocket = require('ws');
var ws = new WebSocket('ws://127.0.0.1:8000/', {origin: 'http://127.0.0.1:8000'});
ws.on('open', function() {
    console.log('connected');
    ws.send(Date.now().toString(), {mask: true});
});
ws.on('close', function() {
    console.log('disconnected');
});
ws.on('message', function(data, flags) {
    console.log('Roundtrip time: ' + (Date.now() - parseInt(data)) + 'ms', flags);
    setTimeout(function() {
        ws.send(Date.now().toString(), {mask: true});
    }, 500);
});

The first option gets the message as 'new message' On the other side the second call throws the following error on the client side. On the server side, both commands pass through a 200OK

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: unexpected server response (200)
    at ClientRequest.<anonymous> (../ws/lib/WebSocket.js:603:17)
    at ClientRequest.g (events.js:175:14)
    at ClientRequest.EventEmitter.emit (events.js:95:17)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (http.js:1689:21)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:120:23)
    at Socket.socketOnData [as ondata] (http.js:1584:20)
    at TCP.onread (net.js:525:27)

On a side note if I log the request.is_websocket() on both calls it returns false meaning on the server side it never goes into the else part.

Please help me understand what mistake I am doing here

Thank you

1 Answer 1

1

Well,

I downloaded their entire code (not pip install) and run the supplied example chat program. Same error. The system sends a 400 response code for any ws:// call. The git hub project page linked on the pypi site returns a 404 error. No way I can file a bug report. Emailed the developer and didn't get any response.

Probably something should have been broken on the new Django 1.5.2 version.

I consider that this is a dead project and hence moving to a complex but working solution like gevent or twisted.

thanks for your support!!!

Sign up to request clarification or add additional context in comments.

3 Comments

Any luck making Django 1.5 or 1.6 work with websockets? do share your experience with twisted and gevent
Hi sj7, we moved the websocket portion completely out of Django and moved to gevent-websocket. From there it was a breeze. From my experience Django or any WSGI based framework is not stable to work with websocket implementation
Hi Jega, thanks for sharing. Planning to move to a node.js + socket.io implementation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.