1

I'm trying to use django-websocket-redis and I didn't understand how it works even reading the doc.. The part client (javascript/template) was easy to understand but I want to send data messages from one client to other and i'm blocking here..

Connecting each client :

var ws = new WebSocket('ws://localhost:8000/ws/foobar?subscribe-group');
ws.onopen = function(e) {
        console.log("websocket connected");
    };
    ws.onclose = function(e) {
        console.log("connection closed");
    };

How manage my views.py to create a link between them ? With NodeJS I was using this code to link the clients together :

io.sockets.on('connection', function (socket) {
    var data={"action": "connexion", "session_id": socket.id,};
    socket.emit('message',data);

    socket.on('message', function(socket){
        if (socket.action == "test")
        {
            io.sockets.socket(socket.code).emit('message',{"action": "move"}); 
            //the socket.code is the session_id of the client one transmitted by a form
        }
    });
});

Thanks you.

2
  • never used that app but the /ws/foobar part of the url will be linked via urls.py to a view ...in that view you need the equivalent of your NodeJS code to re-send the incoming message Commented Apr 30, 2014 at 16:10
  • thanks i will try to do that to begin ! Commented May 2, 2014 at 12:10

1 Answer 1

3

The link between your Django view.py and the Websocket loop is the Redis message queue. Imagine to have two separate main loops on the server: One which handles HTTP-requests using the normal Django request handler. The other loop handles the Websockets, with their long living connections. Since you can't mix both loops within the normal Django request handler, you need message queuing, so that they can communicate to each other.

Therefore, in your Django view.py, send the data to the websocket using something like:

def __init__(self):
    self.redis_publisher = RedisPublisher(facility='foo', broadcast=True)

def get(self, request):
    data_for_websocket = json.dumps({'some': 'data'})
    self.redis_publisher.publish_message(RedisMessage(data_for_websocket))

This will publish data_for_websocket on all Websockets subscribed (=listening) using the URL:

ws://example.com/ws/foo?subscribe-broadcast
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks ! It takes time but I understand how it works ! Just two more question : - It is possible to send this data throught websockets unstead of AJAX requests because this kind of data will be send very frequently so I don't want to DDOS myself... ^^ - It is secure for data exchange ? Websockets are protected with SSL or something ? Otherwise, can we secure it ?
Yes, it is possible, but I discourage its use. Why? Because you can't trigger any job on the server. The only practical use case is to store some data in Redis, which at any time can be consumed from a Django job, but you actually can't use the message queue.

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.