Sure. Here’s the most relevant snippets:
defmodule PresenceTestWeb.UserChannel do
use PresenceTestWeb, :channel
alias PresenceTestWeb.Presence
def join("user:lobby", payload, socket) do
if authorized?(payload) do
socket.endpoint.subscribe("user_chat:lobby")
send(self(), :after_join)
{:ok, socket}
else
{:error, %{reason: "unauthorized"}}
end
end
def handle_info(:after_join, socket) do
push socket, "presence_state", Presence.list(socket)
{:ok, _} = Presence.track(socket, 42, %{
online_at: inspect(System.system_time(:seconds))
})
{:noreply, socket}
end
# other code
end
defmodule PresenceTestWeb.UserChatChannel do
use PresenceTestWeb, :channel
alias PresenceTestWeb.Presence
def join("user_chat:lobby", payload, socket) do
if authorized?(payload) do
send(self(), :after_join)
{:ok, socket}
else
{:error, %{reason: "unauthorized"}}
end
end
def handle_info(:after_join, socket) do
push socket, "presence_state", Presence.list(socket)
{:ok, _} = Presence.track(socket, socket.assigns.user_id, %{
online_at: inspect(System.system_time(:seconds))
})
{:noreply, socket}
end
# other code
end
After a client joins the user:lobby
topic I want to track them as present in the user_chat:lobby
topic as well. But currently when there is a user joined I see the following:
iex(5)> PresenceTestWeb.Presence.list("user_chat:lobby")
%{}
iex(6)> PresenceTestWeb.Presence.list("user:lobby")
%{"42" => %{metas: [%{online_at: "1525307553", phx_ref: "8314Df2yf4Q="}]}}
Here’s a link to the full code: https://github.com/axelson/repro_presence_test