Django Channels Disconnect issue

I’m working on a project where we are using Django channels. We have to send a notification in the room if any of the users disconnect. To start with, each room will be limited to 2 users only. I have a utility function inside websocket_disconnect() , to send messages to the room to notify other users. The issue, that notification is being sent for all requests even when a user is sending a message(using receive_json(), send_json() ). Here message is sent, but still the function send_external_msg_to_channel() from websocket_disconnect() is being triggered. I am using AsyncJsonWebsocketConsumer. The front end uses a reconnecting-websocket package.

class CustomConsumer(AsyncJsonWebsocketConsumer):
    async def websocket_disconnect(self, message):
        print("disconnecting")
        await send_external_msg_to_channel(self.room_name, {"type": "send_json", "event": "disconnect"})
        return await super().websocket_disconnect(message)

Any kind of help with the above issue will be of great help.

Thanks in advance!

Have you verified (using the network tab of the browser’s developer tools) that the client isn’t disconnecting / reconnecting on a regular basis? The first thing I’d want to check is that the connection (at the network layer) is as stable as you’re expecting it to be.

I just checked it and it seems to be a frontend issue. A new WebSocket connection is being established every time. I will try to debug in the frontend itself.

hey i am running into the same problem. what did you find out after debugging the frontend? where you able to go around this issue

This is nearly a 2 year old issue. If you’re having an issue with which you would appreciate assistance, I suggest you open a new topic with the relevent details - including the code that you are using to manage the websocket from the client side.

1 Like