push_ex v0.0.1-rc3 PushExWeb.PushPresence
Link to this section Summary
Functions
Extend presence information with additional data
Callback implementation for Phoenix.Presence.handle_diff/2
Callback implementation for Phoenix.Presence.init/1
Returns presences for a topic
Callback implementation for Phoenix.Presence.start_link/1
Track a channel’s process as a presence
Track an arbitary process as a presence
Stop tracking a channel’s process
Stop tracking a process
Update a channel presence’s metadata
Update a process presence’s metadata
Link to this section Functions
Extend presence information with additional data.
When list/1
is used to list all presences of the given topic
, this
callback is triggered once to modify the result before it is broadcasted to
all channel subscribers. This avoids N query problems and provides a single
place to extend presence metadata. You must return a map of data matching the
original result, including the :metas
key, but can extend the map to include
any additional information.
The default implementation simply passes presences
through unchanged.
Example
def fetch(_topic, presences) do
query =
from u in User,
where: u.id in ^Map.keys(presences),
select: {u.id, u}
users = query |> Repo.all() |> Enum.into(%{})
for {key, %{metas: metas}} <- presences, into: %{} do
{key, %{metas: metas, user: users[key]}}
end
end
Callback implementation for Phoenix.Presence.fetch/2
.
Callback implementation for Phoenix.Presence.handle_diff/2
.
Callback implementation for Phoenix.Presence.init/1
.
Returns presences for a topic.
Calls list/2
with presence module.
Callback implementation for Phoenix.Presence.list/1
.
Callback implementation for Phoenix.Presence.start_link/1
.
Track a channel’s process as a presence.
Tracked presences are grouped by key
, cast as a string. For example, to
group each user’s channels together, use user IDs as keys. Each presence can
be associated with a map of metadata to store small, emphemeral state, such as
a user’s online status. To store detailed information, see fetch/2
.
Example
alias MyApp.Presence
def handle_info(:after_join, socket) do
{:ok, _} = Presence.track(socket, socket.assigns.user_id, %{
online_at: inspect(System.system_time(:second))
})
{:noreply, socket}
end
Callback implementation for Phoenix.Presence.track/3
.
Track an arbitary process as a presence.
Same with track/3
, except track any process by topic
and key
.
Callback implementation for Phoenix.Presence.track/4
.
Stop tracking a channel’s process.
Callback implementation for Phoenix.Presence.untrack/2
.
Stop tracking a process.
Callback implementation for Phoenix.Presence.untrack/3
.
Update a channel presence’s metadata.
Replace a presence’s metadata by passing a new map or a function that takes the current map and returns a new one.
Callback implementation for Phoenix.Presence.update/3
.
Update a process presence’s metadata.
Same as update/3
, but with an arbitary process.
Callback implementation for Phoenix.Presence.update/4
.