Phoenix.Socket.Transport move to another process

Working on custom websockets with Phoenix.

Here, in the Phoenix.Socket.Transport behaviour that I am using, something interesting is suggested:

If the connection is accepted, the transport can move the connection to another process, if so desires, or keep using the same process. The process responsible for managing the socket should then call init/1.

Then, there is no explanation about how to achieve this in a clean way…

I am expecting thousands of connections. Can anybody elaborate about best practices to make sure every websocket has its own process?

Could it be as simple as this?

  @impl true
  def connect(state) do
    spawn(fn ->
      __MODULE__.init(state)
    end)

    {:ok, state}
  end

Thing is, init gets called twice, probably once per the current process and then another time with the spawned one…

Still, I can clearly see that every websocket connection gets its own process from that point on.

Seems like this is not necessary, completely removing the spawn will end up having each websocket in its own process without the double init call.

It’s likely not described because it depends on the transport how you’d do that. You can look at :gen_tcp.controlling_process/2 for an example.