Just make "automatic unique rooms" for pairs of users. The rest remains unchanged. For example, for example
def get_group_name(user1, user2): return 'chat-{}-{}'.format(*sorted([user1.id, user2.id]))
Give it two user objects, and it returns a unique room for this pair of users, ordered User.id , something like "chat-1-2" for users with User.id "1" and "2" ,.
Thus, a user can connect to several connected devices and receive messages sent between two users.
You can get the authenticated user object from message.user .
For the receiving User object, I just sent username along with the message. Then you can unpack it from message['text'] just like you unpack the actual message.
payload = json.loads(message.content['text']) msg = payload['msg'] sender = message.user receiver = get_object_or_404(User, username=payload['receiver'])
Thus, you can remove all the βroomsβ from your example, including the room table, etc. Because group names are always created on the fly when a message is sent between two users.
Another important thing: one user will connect later than another user and may skip the original messages. Therefore, when you connect, you probably want to check the chat_messages database table, get the last 10 or 20 messages between a pair of users, and send them back. In this way, users can catch up with their past conversation.