Search code examples
djangowebsocketdjango-channels

how does django channels knows that a client has disconnected?


I fear that a client loses connection abruptly and get disconnected without leaving any info to the backend. If this happens, will the socket keep active?


Solution

  • I have found the answer in a issue on the django channels repository:

    • Using websocket auto-ping to periodically assert clients are still connected to sockets, and cutting them loose if not. This is a feature that's already in daphne master if you want to try it, and which is coming in the next release; the ping interval and time-till-considered-dead are both configurable. This solves the (more common) clients-disconnecting-uncleanly issue.

    • Storing the timestamp of the last seen action from a client in a database, and then pruning ones you haven't heard of in a while. This is the only approach that will also get round disconnect not being sent because a Daphne server was killed, but it's more specialised, so Channels can't implement it directly.

    Link to the issue on github