Search code examples
djangodjango-channels

Remove a specific user from a Django Channels group?


I have a couple of Django Channels groups that I use to send various messages to the client with.

Can I remove a specific user from one of these groups, using only the ID of the user that I want to remove?

Another potential option would be to force disconnect the user's connection using just their ID.


Solution

  • I solved this in the end by storing each channel name (self.channel_name) on channel connection, and removing them on disconnect. These are then tied to a Django user object.

    Now, if I want to remove a user from a group, I can just loop over all stored channel names tied to a user object, and run group_discard.