I have the following scenario: Two channels on the same socket one for topic “orders” one for topic “order:uuid”.
Now I want to have a single Presence list to track all the users and to what order are they connected.
I want to track user only when he connects to “order:uuid” channel but to be notified about all the users connected directly when I join the “orders” channel.
def join("orders", _params, socket) do
user = Map.get(socket.assigns, :current_user)
if is_nil(user) do
{:error, "nope not authorized"}
else
send(self(), :after_join)
{:ok, socket}
end
end
def join("order:" <> uuid = topic, _params, socket) when is_binary(uuid) do
user = Map.get(socket.assigns, :current_user)
if not is_nil(user) && authorized?(uuid, user) do
socket = socket |> assign(:uuid, uuid) |> assign(:joined_at, NaiveDateTime.utc_now())
{:ok, _} =
Presence.track(socket, "order:#{uuid}", %{
user_id: user.id,
username: user.name,
order: uuid
})
{:ok, socket}
else
{:error, "nope not authorized"}
end
end
def handle_info(:after_join, socket) do
push(socket, "presence_state", Presence.list(socket))
{:noreply, socket}
end
I want when a user joins the “orders” channel to receive a message with all the other users and to what orders are they connected.
What is strange is that Presence.list sends an empty list although there are users connected to order channel.
Since the presence tracks the socket should not be the same Presence?
Why the list is empty? I can not figure this out. Is the presence independent per channel?