You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I couldn't find in the documentation how to close a websocket when it is no longer of any use. Is this possible? Maybe the documentation just needs a small update?
defmodule MyApp.SocketClient.Socket do
use WebSockex, restart: :temporary
def start_link(state) do
WebSockex.start_link("ws://localhost:4001/socket/live", __MODULE__, state)
end
def handle_cast({:send, {type, msg} = frame}, state) do
{:reply, frame, state}
end
def handle_info(:close_socket, {t_ref, state}) do
:timer.cancel(t_ref)
{:close, {nil, state}}
end
def terminate(_, state) do
exit(:normal)
end
end
Activity
Azolo commentedon Apr 15, 2021
Yeah, there's not a specific place that tells that I guess.
The answer is most of the callbacks have the option to return a
{:close, state}tuple.https://hexdocs.pm/websockex/0.4.3/WebSockex.html#c:handle_cast/2
Azolo commentedon Apr 15, 2021
This is definitely a situation where I don't know where to put that particular piece of documentation.
If you have any ideas, let me know.
vipul2774 commentedon Mar 4, 2022
I tried to return
{:close, new_state}fromhandle_info/2&handle_cast/2but again it starts new socket connection. What am I missing? Thanksvipul2774 commentedon Mar 5, 2022
It was solved by using restart option.