G
G
gromyko212020-09-07 13:56:33
Django
gromyko21, 2020-09-07 13:56:33

Private chat on django chanels how to do it right?

Good day to all. Fell into a stupor. I'm making a real-time chat on django for my project using channels. I was able to make (according to the guide) only one chat room where all users can chat. But I need to implement private messages.
The main suspicions fall on the change in this part of the consumer

def connect(self):
        self.room_name = self.scope['url_route']['kwargs']['room_name']
        self.room_group_name = 'chat_%s' % self.room_name
        async_to_sync(self.channel_layer.group_add)(
            self.room_group_name,
            self.channel_name
        )
        self.accept()

Which translates into a room when you enter any url (the room is always the same).
I didn't include much code that could be related to this. If someone understands the topic - tell me where to show the code so that you can help me.
Once again, the chat works, everything is fine, but you need the opportunity to write to any user. Just in case, I will attach a screenshot of a working chat.5f5611d2b7193486772165.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alternativshik, 2020-09-07
@alternativshik

private messages are made with a room in the name of which there is a user id. That's all there is to it. When entering the chat, enter the common room (if necessary) + personal
self.room_name = f'{self.room_name}_{self.scope["user"].id}'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question