I need to access the query parameter dict from Django Channels.
The url may look like this: ws://127.0.0.1:8000/?hello="world"
How do I retrieve 'world' like this: query_params["hello"]
?
On a websocket connect the message.content dictionary contains the query_string.
import urlparse
def ws_connect(message):
params = urlparse.parse_qs(message.content['query_string'])
hello = params.get('hello', (None,))[0]
The getting started documentation (http://channels.readthedocs.io/en/stable/getting-started.html) implies the query_string is included as part of the message.content path, but it doesn't appear to be the case.
Below is a working consumer.py for the chat application sample where the room is passed in the query string:
import urlparse
from channels import Group
from channels.sessions import channel_session
@channel_session
def ws_message(message):
room = message.channel_session['room']
Group("chat-{0}".format(room)).send({"text": "[{1}] {0}".format(message.content['text'], room)})
@channel_session
def ws_connect(message):
message.reply_channel.send({"accept": True})
params = urlparse.parse_qs(message.content['query_string'])
room = params.get('room',('Not Supplied',))[0]
message.channel_session['room'] = room
Group("chat-{0}".format(room)).add(message.reply_channel)
@channel_session
def ws_disconnect(message):
room = message.channel_session['room']
Group("chat-{0}".format(room)).discard(message.reply_channel)