sam
sam

Reputation: 335

django websockets cannot dispatch message on channel

I am using web sockets with Redis on Django. Django is running fine on macOS server but I started running it on Redhat Linux server and now the server gives me this error whenever I send a package over websockets:

ERROR - server - HTTP/WS send decode error:
    Cannot dispatch message on channel
    u'daphne.response.fzdRCEVZkh!nqhIpaLfWb' (unknown)

Note: while I get the error, the package will be received correctly.

I couldn't find any resources for this error.

I followed official instructions for channels.

Upvotes: 4

Views: 1174

Answers (1)

hoefling
hoefling

Reputation: 66251

According to Andrew Godwin (the developer of the channels package), this message is logged when you have a channel that was disconnected, but not removed from channel group(s):

Ah yes, that's Daphne being a little bit more verbose than before, I need to remove that. Don't worry about it - it's perfectly normal after you disconnect a channel that's still in a group. You might want to add a Group.discard call in a disconnect handler to stop it, though.

Source.

I had the same error, using a custom impl of channels.generic.websockets.WebsocketConsumer. After cleaning up channels from groups in disconnect callback, the message disappeared.

A short example with a class-based consumer: assuming you add clients to the broadcast group named foo on connection establishing. Then, on client disconnect, remove its channel from the group:

from channels import Group
from channels.generic.websockets import JsonWebsocketConsumer

class MyConsumer(JsonWebsocketConsumer):

    groupname = 'foo'

    def connect(self, message, **kwargs):
        # send an accept or the connection will be dropped automatically
        self.message.reply_channel.send({"accept": True})
        # add the channel to the broadcast group
        Group(self.groupname).add(message.reply_channel)
        # do the rest of logic that should happen on connection established
        ...

    def disconnect(self, message, **kwargs):
        Group(self.groupname).discard(message.reply_channel)
        # do the rest of logic that should happen on disconnect
        ...

Upvotes: 3

Related Questions