Plan old websockets in phoenix but without magic

If I understood you right, this is what we have:

  • A client C sends a message to the server S
  • The server forwards this message to a process P
  • This process does something with the client’s message and sends a reply to the client
    Client > Server > Process > Client

What is FancyLib? Why are you defining handle(:data, ...) - there is no such opcode?


Let me introduce an example.

defmodule Socket do
  use Phoenix.Socket

  transport :websocket, Phoenix.Transports.Websocket.Raw

  def handle(:text, message, state) do
    Something.dothing(message)

    :ok        
  end
end

defmodule Something do
  use GenServer

  def start_link do
    # Start a GenServer on this module and give it a name
    GenServer.start_link __MODULE__, [], name: PotatoServer
  end

  def dothing(message) do
    GenServer.call PotatoServer, {:in, message}
  end

  def handle_call({:in, message}, from, state) do
    # message = message received from the client
    # from = socket PID, the process that called the GenServer
    
    IO.inspect message

    send from, {:text, "I received this: #{message}"}

    {:noreply, state}
  end
end

Say you have a GenServer running named PotatoServer. Following the scheme, we would have a client sending a message to the server, this message forwarded to the GenServer process, and finally this GenServer would send a message back to the client. Client > Server > PotatoServer > Client.

Essentially, by name:'ing a GenServer we are able to refer to it by name instead of using its PID, which is what we are doing when .call'ing.