What response is sent on channel.join in Phoenix channels?

Hello,

I am trying to learn how to use Phoenix Channels and trying out the code in the documentation. I tried the following on the client:

 let channel = socket.channel("room:lobby", {token: "roomtoken"})

 channel.join()
        .receive("ok", (resp) => {console.log("Joined successfully:", resp)})
        .receive("error", (resp) => {console.log("Unable to join:", resp)})

The join succeeds as expected, however the resp is empty. I thought the resp is empty on successful join because this has data from socket.assigns. So I used the following on the server:

  @impl true
  def join("room:lobby", payload, socket) do
    if authorized?(payload) do
      socket = assign(socket, :user_id, "bb")
      IO.inspect(socket.assigns, label: "socket assigns")
      {:ok, socket}
    else
      {:error, %{reason: "not authorized"}}
    end
  end

I expected the resp to now contain the {user_id: 'bb'}. But it is still empty.
My question is, on a successful join, what response is sent to the client. Can we add/change this response on the server?

Thank you.

Take a look at the return value types for join/3: Phoenix.Channel — Phoenix v1.6.12

To return a response you need to return {:ok, resp, socket} from the function. assigns are a internal storage for the channel process. They’re not shared with the JS side of things.

Thank you @LostKobrakai. I don’t know how I did not think to look in the docs. I kept searching through the guide, assuming that those were the docs. I even tried {:ok, socket, resp} (which, not surprisingly, gave an error).

Anyhow, thanks again for answering the question.

8 posts were split to a new topic: Sync vs Async channel connection authorization