Django Channels and WebSocket Routing and URL Parameters

2,535 views
Skip to first unread message

Robert Roskam

unread,
Aug 18, 2016, 10:45:27 AM8/18/16
to Django developers (Contributions to Django itself)
It seems like passing parameters through routing for websockets should be something that we want, but I've been surprised that I've seen absolutely 0 comments or questions on this so far.

What am I saying specifically? Let me show you from the popular article that jacobian wrote up on Heroku's blog:

____

Need to hook each of these channels up in routing.py:

from . import consumers

channel_routing = {
    'websocket.connect': consumers.ws_connect,
    'websocket.receive': consumers.ws_receive,
    'websocket.disconnect': consumers.ws_disconnect,
}

Pretty simple stuff: just connect each channel to an appropriate function. Now, let’s look at those functions. By convention we’ll put these functions in aconsumers.py (but, like views, these could be anywhere.)

Let’s look at ws_connect first:

from channels import Group
from channels.sessions import channel_session
from .models import Room

@channel_session
def ws_connect(message):
    prefix, label = message['path'].strip('/').split('/')
    room = Room.objects.get(label=label)
    Group('chat-' + label).add(message.reply_channel)
    message.channel_session['room'] = room.label

___


So everything above is straight from that article. I'd like to focus in on this line:

prefix, label = message['path'].strip('/').split('/').

It seems like this should not be tightly coupled to this consumer. It seems like this should be handled more like this:


from channels.urls import parameters  # I know this doesn't exist
from. import consumers 
channel_routing = {
    parameters('websocket.connect', consumers.ws_connect, path=r'chat/(?P<label>\w+)',
    'websocket.receive': consumers.ws_receive,
   'websocket.disconnect': consumers.ws_disconnect,
}


Then you would be able to consume the result this way:

from channels import Group
from channels.sessions import channel_session
from .models import Room

@channel_session
def ws_connect(message, *args, **kwrags):
    label = kwargs['label']
    room = Room.objects.get(label=label)
    Group('chat-' + label).add(message.reply_channel)
    message.channel_session['room'] = room.label


Thoughts?

Andrew Godwin

unread,
Aug 18, 2016, 1:12:26 PM8/18/16
to django-d...@googlegroups.com
Hi Robert,

That blog post dates from before the URL/parameter matching in channels. You can now do things like this:

chat_routing = [
    route("websocket.connect", chat_connect, path=r"^/(?P<room>[a-zA-Z0-9_]+)/$),
    route("websocket.disconnect", chat_disconnect),
]

routing = [
    # You can use a string import path as the first argument as well.
    include(chat_routing, path=r"^/chat"),
]


Andrew

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscribe@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/eb8bb34d-7c07-4443-960e-fe20a6b2eb90%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert Roskam

unread,
Aug 18, 2016, 1:23:16 PM8/18/16
to Django developers (Contributions to Django itself)
Awesome! I was looking for this way back in early May and couldn't find it. Shame on me for not reading the docs.

Robert Roskam
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages