NFIBrokerage / slipstream

A slick WebSocket client for Phoenix Channels
https://hex.pm/packages/slipstream
Apache License 2.0
155 stars 18 forks source link

After a `join/3`, socket assigns are cleared #58

Closed surrsurus closed 12 months ago

surrsurus commented 12 months ago

Hi, I've been using slipstream for a little bit and it's been great. I ran into one issue that I think might be a bug but I'm not sure. I have the following code in a slipstream-powered genserver:

  @impl true
  def handle_continue(:await_connection, %{assigns: %{partner: partner, uri: uri}} = socket) do
    socket =
      with {:ok, socket} <- connect(uri: uri),
           {:ok, socket} <- await_connect(socket) do
        Logger.debug("Joined #{partner}")
        join(socket, "policy_updates")
      end

    {:noreply, socket}
  end

I assign the partner on init, and it's just an atom, the idea is I'm spinning up multiple of these servers for each partner. If I inspect the socket assigns before and after, I notice that the assigns get cleared after the call to join/3, because my handle_join/2 blows up as it expects a partner in the assigns. IO.inspecting the socket reveals the assigns are there before join/3 and then is an empty map afterwards. I can easily fix this issue by just assigning the partner again after the join, but it felt like something was up. If this is intentional behavior feel free to disregard.

the-mikedavis commented 12 months ago

It looks like the assigns are being discarded because the call to connect/1 is creating a fresh socket. I think you should be able to switch from connect/1 - which calls new_socket if not provided with an existing socket - with connect/2 and the assigns should carry through

surrsurus commented 12 months ago

Ahhhhh that explains it, my bad didn't fully see how connect was working, thanks!