Proper way to close channel from the server side?

In my application, clients establish a websocket connection to a phoenix channel. After some actions, the server needs to close the connection

Right now in order to accomplish this I do in my service module:

MyApp.Endpoint.broadcast("topic", "end_session", %{})

In channel:

  def handle_info("end_session", _params, socket) do
    Logger.debug("Ending session...")
    push socket, "end_session", %{}
    {:stop, :normal, socket}

  def terminate(reason, _socket) do
    Logger.debug"Terminating socket... #{inspect reason}"

and in the socket.js:

channel.on("end_session", payload => {
  console.log("terminating session")
channel.onClose( () => console.log("channel has been closed") )

I’m wondering if this is the proper way to gracefully terminate the channel from the server side?


There is a disconnect event described in user_socket.ex.

  # Socket id's are topics that allow you to identify all sockets for a given user:
  #     def id(socket), do: "user_socket:#{socket.assigns.user_id}"
  # Would allow you to broadcast a "disconnect" event and terminate
  # all active sockets and channels for a given user:
  #     BlahWeb.Endpoint.broadcast("user_socket:#{}", "disconnect", %{})
  # Returning `nil` makes this socket anonymous.

Never used it, but it might be what You are looking for.

1 Like