Consumer classes seem to rely on the self.channel_layer.group_add function to register themselves to different groups. I'm not sure of the use cases for requiring the caller to specify the consumer's channel_name directly, but I'm sure it's just a blind spot for me. For an entry level user like me, though, the following function would help remove room for error and serve as a good quality of life improvement:
class AsyncConsumer:
async def add_to_group(self, group_name: str):
"""
Function used to demystify how group addition to work.
self.channel_layer.group_add(<group_name>, self.channel_name) should generally be called like that. The only
variation should be the group name.
Args:
group_name: The name of the group that this instance of this consumer should belong to
"""
await self.channel_layer.group_add(group_name, self.channel_name)
This would simplify some of the example code to look like
Consumer classes seem to rely on the
self.channel_layer.group_add
function to register themselves to different groups. I'm not sure of the use cases for requiring the caller to specify the consumer'schannel_name
directly, but I'm sure it's just a blind spot for me. For an entry level user like me, though, the following function would help remove room for error and serve as a good quality of life improvement:This would simplify some of the example code to look like
instead of
It doesn't seem like a lot, but it reduces cognitive complexity.