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.