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:
https://blog.heroku.com/in_deep_with_django_channels_the_future_of_real_time_apps_in_django
 

____

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?

-- 
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 [email protected].
To post to this group, send email to [email protected].
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.

Reply via email to