django-channels get the json post on connect

121 views
Skip to first unread message

Samuel Muiruri

unread,
Aug 31, 2017, 1:08:14 PM8/31/17
to Django users
I have this in routing.py for django-channels

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

and the ws_connect looks like this

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

and when the page loads this socket connection is made correctly

<script type="text/javascript">
// Note that the path doesn't matter for routing; any WebSocket
// connection gets bumped over to WebSocket consumers
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();
</script>

I want to start testing if I can have a background process running from this, so I set a test json input to socket.send however I haven't found anything on the documentation, apologies if I missed it. How to access this info in ws_connect it should be in message same way there's request I believe.

Ryan Nowakowski

unread,
Sep 14, 2017, 9:56:49 AM9/14/17
to Samuel Muiruri, Django users
IIRC, that test message will be available in your ws_message function.
Reply all
Reply to author
Forward
0 new messages