Phoenix's Channels

Tonight, I explored channels in Phoenix. Being new to Phoenix & Elixir, I wanted to know about the underlying processes that Phoenix is spinning up. From what I can tell, Phoenix creates a channel unique to each browser and channel.

So, if I were to join the chatrooms called "rooms:support" and "room:lobby", Phoenix would create two channels that are both unique to my browser. Each channel would also create a socket.

I'm still unsure where state is stored -- is it on the channel or the socket? If it's on the socket, it seems it would be stored here:

  @doc """
  Adds key/value pair to ephemeral socket state

  ## Examples

      iex> socket = Socket.put_topic(%Socket{}, "rooms:lobby")
      %Socket{topic: "rooms:lobby"}
      iex> socket.assigns[:token]
      iex> socket = Socket.assign(socket, :token, "bar")
      iex> socket.assigns[:token]

  def assign(socket = %Socket{}, key, value) do
    put_in socket.assigns[key], value

However, with Elixir being immutable, I'm unsure how put_in modified the state of the socket.

I've been learning a bit about the supervision tree concept in Elixir. I'd be really curious to see a diagram of the supervision tree as it applies to channels. Hopefully, it would help me understand where state is kept and how the data flows. I'm having a hard time fully grocking it as is.

Also, I'm curious the difference between a channel and a socket and how they're used. Hopefully, I'll find out in the days ahead.

Scott Messinger

Read more posts by this author.