Getting message for disconnection of client or Phoenix server

My client and server are communicating consecutively but I want a method that notify when client disconnect from server in handle_in function. As handle_in function is providing response back and everything.
Here’s my handle_in fucntion in room_channel:

def handle_in("message:add", %{"message" => body}, socket) do
    room_id = socket.assigns[:room_id]
    broadcast!(socket, "room:#{room_id}:new_message", %{body: body})
    IO.puts("Received Message from Client: #{body}")
    {:reply, {:ok, %{from: "server", body: body}}, socket}

This is how I’m getting response back in client from server:

def respond(payload):

channel.push("message:add", {"message": "HELLO WORLD"}, respond)

Pheonix → Phoenix

1 Like

There’s interesting discussions about this:

I saw a snippet in auto generated auth code: DerpyCoderWeb.Endpoint.broadcast(live_socket_id, “disconnect”, %{})

That broadcasts disconnect event on logout.

So I was thinking that same event gets fired for socket disconnect, which we can handle.

And of course we have to lower the heartbeat interval, so we can detect when client disconnects quickly.

How can I add the disconnection functionality in handle_in function. Because my handle_in function is joining and responding back to the client.