Can Phoenix channel detect client offline immediately?Like WiFi disconnected

I need to know whether the client is online, when WiFi is down.
The server should know it immediately. Can anyone tell me an elegant solution?

Now as I tested, Phoenix Channel needs about 40-50s to detect the client is disconnected.

1 Like

Welcome @WalkPhoneGo. The server should instantly know when a client is no longer connected, if the disconnection was clean. If it was not clean and stayed open on the server side, it would take a heartbeat cycle for it to realize that the connection is no longer present. Phoenix Channels are processes and will die when the parent Socket handler dies, which happens when the connection is severed.

I’ve only really seen clean disconnects in the wild. I did see phantom connections once, but it was on a load test when the execution server did not kill its client connections properly.

1 Like

An instant event in this case is technically impossible.

Only the client knows about the cut down WiFi. But since WiFi is down, it can’t tell the server.

There might even be situations in which neither of the participants know about the disconnect. Lets say your ISP drops the connection. Neither your client nor your server will know this until they try the next heartbeat and it times out.

This is just how networking works.


Thanks, that means I have to implement a PING-PONG mechanism to continuously check whether the client is offline.

Since it already has a ping from the server, I just need to implement pong from client to server. Am I right?

tks, looks I have to make it by myself…:expressionless:

While it cannot be “instantaneous”, it definitely doesn’t need to be a full 40-50 seconds to detect an offline state from the server.

This shouldn’t be necessary since Phoenix Channel’s have heartbeat support built-in, and you can tweak the interval.

From the phoenix js docs: you want to tweak heartbeatIntervalMs. Although in the normal case of someone closing the tab that should be detected without waiting for the heartbeat.


You make me confused, it means Phoenix channel client will send heartbeats to the server in default.

If that so, why server need 40-50s to know the client is offline due to the WiFi down?

The connection isn’t severing cleanly in this case. One side of the connection thinks it is online (server) while the other side (client) is certainly not online. Many load balancers / proxies will kill connections if they don’t send data in so many seconds. The heartbeat process normally works 2 fold: it keeps the connection alive in your load balancer, and it lets the Phoenix server (Cowboy) know that the client is still connected.

The duration of this heartbeat defaults to 30s ( You can change this to a lower number if you want to know about a disconnection sooner. However, that will come at the cost of additional messages / processing / bandwidth. It’s probably fine for a smaller application.


I change the default value to 3s, but the server still needs 40-50s to detect client offline.
I think the server-side have to handle the heartbeat. And sadly I don’t know how to do it.

I found this from phoenix source code

so how can I handle this in my code?

You need to also set the webserver’s idle timeout to a lower value, ie

config :app, AppWeb.Endpoint, 
  http: [
    protocol_options: [
      idle_timeout: 10_000

Yes!!!, that’s what I need. work like a charm!!!!!!

Thank you!

I was fed up with the Actionable so I chose Phoenix Channel,it’s like magic!!!