mirror of
https://github.com/fly-apps/live_beats.git
synced 2024-05-20 10:08:07 +00:00
9998e06caa
Avoid fetching each user by passing in full pre-fetched presences from Presence.fetch/2 callback. Use temporary assigns in ProfileLive to avoid duping presences in memeory. Handle removes by a small hook event
58 lines
1.1 KiB
Elixir
58 lines
1.1 KiB
Elixir
defmodule LiveBeats.PresenceClient do
|
|
@behaviour Phoenix.Presence.Client
|
|
|
|
@presence LiveBeatsWeb.Presence
|
|
@pubsub LiveBeats.PubSub
|
|
|
|
def list(topic) do
|
|
@presence.list(topic)
|
|
end
|
|
|
|
@impl Phoenix.Presence.Client
|
|
def init(_opts) do
|
|
# user-land state
|
|
{:ok, %{}}
|
|
end
|
|
|
|
@impl Phoenix.Presence.Client
|
|
def handle_join(topic, _key, presence, state) do
|
|
active_users_topic =
|
|
topic
|
|
|> profile_identifier()
|
|
|> active_users_topic()
|
|
|
|
Phoenix.PubSub.local_broadcast(
|
|
@pubsub,
|
|
active_users_topic,
|
|
{__MODULE__, %{user_joined: presence}}
|
|
)
|
|
|
|
{:ok, state}
|
|
end
|
|
|
|
@impl Phoenix.Presence.Client
|
|
def handle_leave(topic, _key, presence, state) do
|
|
active_users_topic =
|
|
topic
|
|
|> profile_identifier()
|
|
|> active_users_topic()
|
|
|
|
Phoenix.PubSub.local_broadcast(
|
|
@pubsub,
|
|
active_users_topic,
|
|
{__MODULE__, %{user_left: presence}}
|
|
)
|
|
|
|
{:ok, state}
|
|
end
|
|
|
|
defp active_users_topic(user_id) do
|
|
"active_users:#{user_id}"
|
|
end
|
|
|
|
defp profile_identifier(topic) do
|
|
"active_profile:" <> identifier = topic
|
|
identifier
|
|
end
|
|
end
|