[Answered ]-Is it necessary to make the channel room name unique in django channels? I have a function which works fine but have some concerns

1👍

If I was implementing this, I would use the username(which is assumed to be unique) as the room name. The implementation would be something like this,

from collections import defaultdict

class LiveFeedConsumer(WebsocketConsumer):
    #  map which uses the subscribed user's username as a key and stores the set of all users' username's who subscribed to their feeds as value.
    # Ex - users 1, 2, 3, 4 are available
    # __subscriptions[1] = {2, 3} -> Means 2 and 3 have subscribed to 1's feeds
    __subscriptions = defaultdict(set)

    def connect(self):
        # authenticate
        user_to_which_to_subscribe_to = get_user_somehow()
        self.scope["session"]["room_name"] = user_to_which_to_subscribe_to.username

        # Accept connection
        self.__subscriptions[user_to_which_to_subscribe_to.username].add(self.scope["user"].username) # To extract users and their subscriptions

    def disconnect(self, message):
        # Disconnection logic
        self.__subscriptions[ self.scope["session"]["room_name"] ].remove(self.scope["user"].username)

    def get_subs_status(self, user, other_user):
        return user.username in self.__subscriptions[other_user.username]

    def external_feed(self, event): # sending from outside the consumer
        user = self.user
        oth_user = event['user']
        condition = self.get_subs_status(user, oth_user)
        if condition is True:
            self.send_json({
                'newFeed': event['feeds'],
            })

        # publish to pub/sub mechanism to support multiple processes/nodes

You can add the async/await parts of the code. You get the logic.

This way you’ll have separate rooms for each user’s live feed. Any user can subscribe to multiple other users. Furthermore, you can add a pub/sub listener to scale this horizontally, adding multiple nodes/processes.

Leave a comment