Processing data sent by sockets in django-channels

36 views
Skip to first unread message

Samuel Muiruri

unread,
Sep 4, 2017, 4:48:06 AM9/4/17
to django...@googlegroups.com
According to django-channels' github any "help" related issue should be posted here, I've asked this question before here and on stack with no response, don't feel the need to but if I don't get an adequate response I might need to hire a dev to implement this for me.

I have this code for a socket connection

socket = new WebSocket("ws://" + window.location.host + "/chat/");
socket.onmessage = function(response) {
   console.log(response.data);
}
socket.onopen = function() {
   socket.send({"test":"data"});
}
// Call onopen directly if socket is already open
if (socket.readyState == WebSocket.OPEN) socket.onopen();

works nicely and connects to this route (even though it's supposed to look for /chat/ but I can work on setting this later)

from gallery.consumers import ws_connect, ws_message

channel_routing = [
   # Wire up websocket channels to our consumers:
   route("websocket.connect", ws_connect),
   route("websocket.receive", ws_message),
]

and in the consumers file

import json
from channels import Group
from channels.sessions import channel_session

# Connected to websocket.connect
@channel_session
def ws_connect(message, key):
    # Accept connection
    message.reply_channel.send({"accept": True})

# Connected to websocket.receive
@channel_session
def ws_message(message, key):
    reply = json.dumps({'status':'ok'})
    message.reply_channel.send({"text": reply})

# Connected to websocket.disconnect
@channel_session
def ws_disconnect(message, key):
    message.reply_channel.send({'accept': False})

first hurdle I need to get over is getting the data sent via sockets i.e. 

socket.onopen = function() {
    socket.send({"test":"data"});
}

then I can move to having this trigger a background process so I can send the status and finally the final result via sockets.

--
Best Regards,
Samuel Muiruri.
Web Designer | +254 738 940064

Andrew Godwin

unread,
Sep 4, 2017, 4:36:26 PM9/4/17
to django...@googlegroups.com
Hi,

I don't keep track of stack overflow so I don't know if anybody else would answer stuff there.

Secondly, I replied to a very similar question you asked on this list last week - is this the same problem as that?

Andrew

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJZFZXoiq3WQ5zaXGMSxYpAfCREqQtC-73yTL8-9OED9vA0m4Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages