send messages to user tracker from presence client

This commit is contained in:
Berenice Medel 2022-01-25 13:39:27 -06:00
parent b440ec24ce
commit f23539c6da
3 changed files with 26 additions and 17 deletions

View file

@ -29,7 +29,9 @@ defmodule LiveBeats.Application do
name: PresenceClient}, name: PresenceClient},
# Start the Endpoint (http/https) # Start the Endpoint (http/https)
LiveBeatsWeb.Endpoint, LiveBeatsWeb.Endpoint,
{LiveBeats.SongsCleaner, count: 7, interval: :day} {LiveBeats.SongsCleaner, count: 7, interval: :day},
LiveBeats.UserTracker
# Start a worker by calling: LiveBeats.Worker.start_link(arg) # Start a worker by calling: LiveBeats.Worker.start_link(arg)
# {LiveBeats.Worker, arg} # {LiveBeats.Worker, arg}

View file

@ -41,12 +41,14 @@ defmodule LiveBeats.PresenceClient do
@impl Phoenix.Presence.Client @impl Phoenix.Presence.Client
def handle_join(topic, _key, presence, state) do def handle_join(topic, _key, presence, state) do
LiveBeats.UserTracker.presence_joined(presence)
local_broadcast(topic, {__MODULE__, %{user_joined: presence}}) local_broadcast(topic, {__MODULE__, %{user_joined: presence}})
{:ok, state} {:ok, state}
end end
@impl Phoenix.Presence.Client @impl Phoenix.Presence.Client
def handle_leave(topic, _key, presence, state) do def handle_leave(topic, _key, presence, state) do
LiveBeats.UserTracker.presence_left(presence)
local_broadcast(topic, {__MODULE__, %{user_left: presence}}) local_broadcast(topic, {__MODULE__, %{user_left: presence}})
{:ok, state} {:ok, state}
end end

View file

@ -13,8 +13,16 @@ defmodule LiveBeats.UserTracker do
Phoenix.PubSub.subscribe(@pubsub, topic()) Phoenix.PubSub.subscribe(@pubsub, topic())
end end
def start_link(_opts) do def start_link(opts) do
GenServer.start_link(__MODULE__, name: __MODULE__) GenServer.start_link(__MODULE__, opts, name: __MODULE__)
end
def presence_joined(presence) do
GenServer.call(__MODULE__, {:presence_joined, presence})
end
def presence_left(presence) do
GenServer.call(__MODULE__, {:presence_left, presence})
end end
@impl true @impl true
@ -23,13 +31,13 @@ defmodule LiveBeats.UserTracker do
end end
@impl true @impl true
def handle_call({:user_joined, user}, _from, state) do def handle_call({:presence_joined, presence}, _from, state) do
{:reply, :ok, handle_join(state, user) |> IO.inspect} {:reply, :ok, handle_join(state, presence)}
end end
@impl true @impl true
def handle_call({:user_left, user}, _from, state) do def handle_call({:presence_left, presence}, _from, state) do
{:reply, :ok, handle_leave(state, user) |> IO.inspect} {:reply, :ok, handle_leave(state, presence)}
end end
@impl true @impl true
@ -43,17 +51,17 @@ defmodule LiveBeats.UserTracker do
state state
end end
defp handle_join(state, user) do defp handle_join(state, presence) do
if Map.has_key?(state, user.id) do if Map.has_key?(state, presence.user.id) do
state state
else else
Map.put_new(state, user.id, user) Map.put_new(state, presence.user.id, presence.user)
end end
end end
defp handle_leave(state, user) do defp handle_leave(state, presence) do
if Map.has_key?(state, user.id) do if Map.has_key?(state, presence.user.id) and presence.metas == [] do
Map.delete(state, user.id) Map.delete(state, presence.user.id)
else else
state state
end end
@ -68,10 +76,7 @@ defmodule LiveBeats.UserTracker do
state state
|> Enum.map(fn {_key, value} -> value end) |> Enum.map(fn {_key, value} -> value end)
Logger.info( Phoenix.PubSub.local_broadcast(@pubsub, topic(), {LiveBeats.UserTracker, %{active_users: active_users}})
"broadcasting updates"
)
Phoenix.PubSub.local_broadcast(@pubsub, topic(), active_users)
end end
end end