Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to shutdown/close websocket? #93

Open
grantwest opened this issue Mar 26, 2021 · 4 comments
Open

How to shutdown/close websocket? #93

grantwest opened this issue Mar 26, 2021 · 4 comments

Comments

@grantwest
Copy link

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?

@Azolo
Copy link
Owner

Azolo commented 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
Copy link
Owner

Azolo commented 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
Copy link

vipul2774 commented Mar 4, 2022

I tried to return {:close, new_state} from handle_info/2 & handle_cast/2 but again it starts new socket connection. What am I missing? Thanks

@vipul2774
Copy link

vipul2774 commented Mar 5, 2022

It was solved by using restart option.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants