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}
end
def terminate(reason, _socket) do
Logger.debug"Terminating socket... #{inspect reason}"
:ok
end
and in the socket.js:
channel.on("end_session", payload => {
console.log("terminating session")
channel.leave()
})
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?
Thanks!