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
On Thursday, August 18, 2016 at 1:12:26 PM UTC-4, Andrew Godwin wrote:
>
> 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"),
> ]
>
> You can read more in the docs here:
> http://channels.readthedocs.io/en/latest/getting-started.html#routing
>
> Andrew
>
> On Thu, Aug 18, 2016 at 7:45 AM, Robert Roskam <[email protected]
> <javascript:>> wrote:
>
>> 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] <javascript:>.
>> To post to this group, send email to [email protected]
>> <javascript:>.
>> 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
>>
>> <https://groups.google.com/d/msgid/django-developers/eb8bb34d-7c07-4443-960e-fe20a6b2eb90%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
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/701a628a-fa2f-45c4-a70a-d8169fcc7d33%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.