live_beats/lib/live_beats/presence/presence_client.ex

49 lines
1 KiB
Elixir
Raw Normal View History

2021-12-09 05:33:29 +00:00
defmodule LiveBeats.PresenceClient do
@behaviour Phoenix.Presence.Client
@presence LiveBeatsWeb.Presence
@pubsub LiveBeats.PubSub
2021-12-09 05:33:29 +00:00
def list(topic) do
@presence.list(topic)
end
@impl Phoenix.Presence.Client
2021-12-09 05:33:29 +00:00
def init(_opts) do
# user-land state
{:ok, %{}}
end
@impl Phoenix.Presence.Client
def handle_join(topic, key, _meta, state) do
active_users_topic =
topic
|> profile_identifier()
|> active_users_topic()
Phoenix.PubSub.local_broadcast(@pubsub, active_users_topic, {__MODULE__, %{user_joined: key}})
2021-12-09 05:33:29 +00:00
{:ok, state}
end
@impl Phoenix.Presence.Client
def handle_leave(topic, key, _meta, state) do
active_users_topic =
topic
|> profile_identifier()
|> active_users_topic()
Phoenix.PubSub.local_broadcast(@pubsub, active_users_topic, {__MODULE__, %{user_left: key}})
2021-12-09 05:33:29 +00:00
{: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
2021-12-09 05:33:29 +00:00
end