I'm using Django channels as an intermediate agent, which passes data from one browser(parent/sender) to other connected browsers(children/receivers). And in my consumers, I do a channel_layer.group_send(data) once data are received from the parent browser, so that children browsers can get the data from redis channel later.
However, what I really want is the data passed to the channel should be received by all the children, except the parent browser. My question is, how to exclude the data sender in the group?
Unfortunately, django channels does not offer a filtering like that. I have solved the problem by checking in the chat_message function whether the current connection is the sender.
async def receive(self, text_data):
text_data_json = json.loads(text_data)
# Send message to room group
await self.channel_layer.group_send(
self.GROUP_NAME,
{
'type': 'chat_message',
'data': text_data_json,
'sender_channel_name': self.channel_name
}
)
# Receive message from room group
async def chat_message(self, event):
# send to everyone else than the sender
if self.channel_name != event['sender_channel_name']:
await self.send(text_data=json.dumps(event))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With