I am working on a chat app that has Rooms. Each room has two users. A user can be in multiple rooms i.e, a user has multiple rooms. And now he is chatting in one room. But he receives a message in another room. I would like to notify about the message from other room to the user. How should I implement this?
Currently a websocket connection is established as: ws://localhost:8000/chat/int<room_id>/
And the group_name is named as "room"+room_id
. and So far I have:
async def connect(self):
room_id = self.scope['url_route']['kwargs']['room_id']
await self.channel_layer.group_add(
"room"+room_id,
self.channel_name
)
await self.accept()
async def receive(self, text_data):
await self.channel_layer.group_send(
self.room_name,
{
'type': 'chat_message',
'message': json.loads(text_data)
}
)
async def chat_message(self, event):
await self.send(text_data=json.dumps({
'message': event['message']
}))
Django 2.x django-channels 2.x python 3.6