Channels: Get a list of Channels in a Group

Created on 23 Aug 2016  路  5Comments  路  Source: django/channels

I've searched the docs and google for a solution, what I'm trying to do is get a list of channels in a group, so that I can see who is receiving the messages that I send to a Group.

This will also allow things like merging groups:

from channels import Channel, Group

old_group = Group('old_friends')
old_group.add(Channel('alex'))
old_group.add(Channel('bob'))

new_group = Group('new_friends')
new_group.add(Channel('lisa'))

[new_group.add(channel) for channel in old_group.channels]  # add the people in the old group to the new

for channel in new_group.channels:
     print(channel.name)
# alex
# bob
# lisa

Is there an API to do this already, and if not, can we add one? It seems like Group.channel_layer.router.channels is not what I'm looking for.

Most helpful comment

please ref. my comment to #174 (https://github.com/django/channels/issues/174#issuecomment-288863419) : any comment to what "pretty small" could mean ? How many channels need to be in a group for the Redis using lua be noticeably better than a simple python loop ...

All 5 comments

There is a way to get it - a raw ASGI endpoint called group_channels, which you can find in the spec here: http://channels.readthedocs.io/en/latest/asgi.html#specification-details

It's not exposed at a higher level as it's designed to be a very slow endpoint, and should not be used in any sort of live code if you can avoid it. I urge you to consider other ways of handling what you want to do - for example, if you want room presence, managing it externally using a database.

Followup question: I have a similar problem, and after reading your recommendations (in #293 particularly), have rolled my own presence management.

At this point, there is no functional need to use groups at all - I can just iterate over my Presences and send a message to each's reply_channel. Assuming accessing the full list of Presences is itself performant and/or necessary for each message, Is it a bad idea (e.g. for performance reasons on the channel layer) to do so?

Group sends are faster as they're done natively in Redis using a lua script, but as long as your sets of users are pretty small per presence it won't be much difference at all.

How can I get Group list?

please ref. my comment to #174 (https://github.com/django/channels/issues/174#issuecomment-288863419) : any comment to what "pretty small" could mean ? How many channels need to be in a group for the Redis using lua be noticeably better than a simple python loop ...

Was this page helpful?
0 / 5 - 0 ratings