How can I know why the websocket connection failed / was terminated?

Is there any way for the client to know why the websocket connection failed. For instance I’m wondering how to differentiate between, the server being down, the connection being rejected by the connect function and multiple reasons the connection could be closed by the server using the disconnect event (Endpoint.broadcast("users_socket:#{}", "disconnect", %{})). I am using a token stored in the localStorage to connect to the websocket and authenticate in the connect function.

A hard disconnect is default because it is the ‘safe’ thing to do (otherwise you could easily spam the outgoing network if something happens). If you want to notify why a connection is being disconnected and then disconnect then you need to send a message to the client the reason, wait a touch to let it get processed (usually sending a message to self() is fine) then die as is usual with fairly raw network connections like websocket. :slight_smile: