Channels: Duplicate websocket messages

Created on 1 Dec 2017  路  6Comments  路  Source: django/channels

  • Your OS and runtime environment, and browser if applicable

Browser: chrome, OS: ubuntu

  • The versions of Channels, Daphne, Django, Twisted, and your ASGI backend (asgi_ipc or asgi_redis normally)

asgi-redis==1.4.3
channels==1.1.8
Django==1.10.4
Twisted==16.6.0

  • What you expected to happen vs. what actually happened

I am receiving duplicate messages in my consumer. I have a java client that stays connected and attempts to reconnect at all times to the websocket. I also have web clients sending messages to websocket to a dedicated channels group just for them, which is then resent to a dedicated channels group for the java process. Especially if I restart daphne, runworker and celery a lot, I receive duplicate messages (or triplicate, or nplicate, you get the idea) from my web clients. I have verified that the messages are not sent twice by the clients. Note that I've logged the duplicate messages in python (def ws_message(message)), not just in my java process, which consequently also receives duplicates. Once I start to receive duplicates I have to restart the processes, and then it's ok until I restart it again. It seems rather random, the fact if I do or do not get duplicates within a lifecycle of the channels app, but the issue is never resolved without a restart. I receive n-plicates from all my clients, which are many.

  • How you're running Channels (runserver? daphne/runworker? Nginx/Apache in front?)

I'm running it behind nginx with daphne & runworker.

  • Console logs and full tracebacks of any errors

No errors really, just duplicates.

This is how I'm logging my duplicates:

channel_routing = [
    route("websocket.connect", ws_add),
    route("websocket.receive", ws_message),
    route("websocket.disconnect", ws_disconnect),
]

def ws_add(message):
    room = re.sub("/websocket/", '', message.content['path'])
    message.reply_channel.send({"accept": True})
    Group(str(room)).add(message.reply_channel)

def ws_message(message):
    room = re.sub("/websocket/", '', message.content['path'])
    text = message.content['text']
    txt = json.loads(text)
    print(txt)

def ws_disconnect(message):
    Group("java").discard(message.reply_channel)
    Group("web").discard(message.reply_channel)

I am using reconnecting-websocket (https://github.com/joewalnes/reconnecting-websocket) to connect to it via frontend.

blockeneeds-investigation bug exadvanced

Most helpful comment

Channels 2 is released, closing this as the transport is now not over a network and so this won't happen.

All 6 comments

I've had a report of this before but it did end up being a client error. How have you verified the browser process is not sending duplicate messages?

There's one websocket connection opened if I view it in the developer console, and the message only gets sent once. Also it happens on all my clients, consistently. The number of messages that is received within a lifecycle matches across all clients.

If there's any additional debug info that I could provide, let me know.

I've been using channels in production for over a year now, and this issue persists even though I regularly update all the package versions. I have never experienced it in my development setup, though.

I need steps to reproduce if I'm going to be able to fix it, unfortunately. I'm going to put this into blocked for now until we can work out how to do that (or until channels 2 releases and this likely goes away in the new architecture)

@andrewgodwin When do you anticipate channels 2 will be released? We have moved our Django+DRF to daphne and are starting to incorporate websockets on every new feature, but unsure whether to pause if the new release will be out in 2-3 months. Or, is the new released planned to be out in something like 4-6 or more months? I know it's an ungrateful question so I apologize for that.

@bkovacev Hard deadlines are not something a volunteer project has, but it should be more like 2 months than 6.

Channels 2 is released, closing this as the transport is now not over a network and so this won't happen.

Was this page helpful?
0 / 5 - 0 ratings