Adding multiple groups with django channels and websocket

997 Views Asked by At

I try to development a notificaiton system. I would like the system administrator to be able to send notifications to different groups. For this, I create only one websocket connection, and when socket is connected(during login) I want to add the user to the groups it belongs to. I wrote the code as below but I'm not sure it's correct. I am already getting this error: "AttributeError: 'WebSocketProtocol' object has no attribute 'handshake_deferred'"

class MyConsumer(AsyncWebsocketConsumer):
    async def connect(self):
        if self.scope["user"].is_anonymous:
            await self.close()
        else:
            groups = await sync_to_async(list)(self.scope["user"].channel_groups.all())
            for group in groups:
                await self.channel_layer.group_add(group.name,self.channel_name)
                await self.accept()
        print("###CONNECTED")

Can you help me? Am i on the right way? If so how do i fix this error?

0

There are 0 best solutions below