How to determine to message to particular set of listeners?
For example, I have Luke,Jason and Bruce in ChatRoom A. Lisa in Chatroom B.And each member has a message list screen which only display the last message of group chat room.
I have a socket connection placed on every chatroom to listen incoming messages. Group messages from particular chatroom will have an room identifier along with message. The question is that how websocket to determine to send out messages only to those people who has subscribed to this chatroom?
This websocket conncetion will receive msgs from diffrent room but it needs only to send msgs out to those who belongs to that particular group chat room.
Do I need add some identifier in somewhere when I establish connection from client side so that serverside could know if it needs to send msgs to me?
Here is my connect function:
class GroupMessageListConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.userId = str(self.scope['query_string'])[2:-1].split("=")[1]
# self.eventId = self.scope['url_route']['kwargs']['eventId']
print('GroupMessageListConsumer connected self.userId:{}'.format(self.userId))
# print('GroupMessageListConsumer connected self.userId:{}'.format(self.userId))
# print('GroupMessageListConsumer connected self.eventId:{}'.format(self.eventId))
# Join room group
await self.channel_layer.group_add(
self.userId,
self.channel_name
)
await self.accept()
async def disconnect(self, close_code):
# Leave room group
await self.channel_layer.group_discard(
self.userId,
self.channel_name
)
room identifieralong with the message.{"room": 123, "message": "hi"}. Then within your consumer'sreceivemethod check for appropriate room and pass on. - whitehatconnectmethod, you need to check and add the user to their respective rooms. - whitehat