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.init/1

Returns presences for a topic

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

Link to this function fetch(topic, presences)

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.

Link to this function get_by_key(topic, key)
Link to this function handle_diff(diff, state)

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.

Link to this function listeners?(topic)
Link to this function start_link(opts \\ [])

Callback implementation for Phoenix.Presence.start_link/1.

Link to this function track(socket, key, meta)

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.

Link to this function track(pid, topic, key, meta)

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.

Link to this function untrack(socket, key)

Stop tracking a channel’s process.

Callback implementation for Phoenix.Presence.untrack/2.

Link to this function untrack(pid, topic, key)

Stop tracking a process.

Callback implementation for Phoenix.Presence.untrack/3.

Link to this function update(socket, key, meta)

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.

Link to this function update(pid, topic, key, meta)

Update a process presence’s metadata.

Same as update/3, but with an arbitary process.

Callback implementation for Phoenix.Presence.update/4.