Phoenix v1.3.0 Phoenix.Presence behaviour View Source

Provides Presence tracking to processes and channels.

This behaviour provides presence features such as fetching presences for a given topic, as well as handling diffs of join and leave events as they occur in real-time. Using this module defines a supervisor and allows the calling module to implement the Phoenix.Tracker behaviour which starts a tracker process to handle presence information.

Example Usage

Start by defining a presence module within your application which uses Phoenix.Presence and provide the :otp_app which holds your configuration, as well as the :pubsub_server.

defmodule MyApp.Presence do
  use Phoenix.Presence, otp_app: :my_app,
                        pubsub_server: MyApp.PubSub
end

The :pubsub_server must point to an existing pubsub server running in your application, which is included by default as MyApp.PubSub for new applications.

Next, add the new supervisor to your supervision tree in lib/my_app.ex:

children = [
  ...
  supervisor(MyApp.Presence, []),
]

Once added, presences can be tracked in your channel after joining:

defmodule MyApp.MyChannel do
  use MyAppWeb, :channel
  alias MyApp.Presence

  def join("some:topic", _params, socket) do
    send(self(), :after_join)
    {:ok, assign(socket, :user_id, ...)}
  end

  def handle_info(:after_join, socket) do
    push socket, "presence_state", Presence.list(socket)
    {:ok, _} = Presence.track(socket, socket.assigns.user_id, %{
      online_at: inspect(System.system_time(:seconds))
    })
    {:noreply, socket}
  end
end

In the example above, the current presence information for the socket’s topic is pushed to the client as a "presence_state" event. Next, Presence.track is used to register this channel’s process as a presence for the socket’s user ID, with a map of metadata.

Finally, a diff of presence join and leave events will be sent to the client as they happen in real-time with the “presence_diff” event. The diff structure will be a map of :joins and :leaves of the form:

%{joins: %{"123" => %{metas: [%{status: "away", phx_ref: ...}]},
  leaves: %{"456" => %{metas: [%{status: "online", phx_ref: ...}]},

See Phoenix.Presence.list/2 for more information on the presence data structure.

Fetching Presence Information

Presence metadata should be minimized and used to store small, ephemeral state, such as a user’s “online” or “away” status. More detailed information, such as user details that need to be fetched from the database, can be achieved by overriding the fetch/2 function. The fetch/2 callback is triggered when using list/1 and serves as a mechanism to fetch presence information a single time, before broadcasting the information to all channel subscribers. This prevents N query problems and gives you a single place to group isolated data fetching to extend presence metadata. The function must return a map of data matching the outlined Presence data structure, including the :metas key, but can extend the map of information to include any additional information. For example:

def fetch(_topic, entries) do
  query =
    from u in User,
      where: u.id in ^Map.keys(entries),
      select: {u.id, u}

  users = query |> Repo.all |> Enum.into(%{})

  for {key, %{metas: metas}} <- entries, into: %{} do
    {key, %{metas: metas, user: users[key]}}
  end
end

The function above fetches all users from the database who have registered presences for the given topic. The fetched information is then extended with a :user key of the user’s information, while maintaining the required :metas field from the original presence data.

Link to this section Summary

Functions

Returns presences for a topic

Link to this section Types

Link to this type presence() View Source
presence() :: %{key: String.t, meta: map}
Link to this type presences() View Source
presences() :: %{optional(String.t) => %{metas: [map]}}

Link to this section Functions

Returns presences for a topic.

Presence data structure

The presence information is returned as a map with presences grouped by key, cast as a string, and accumulated metadata, with the following form:

%{key => %{metas: [%{phx_ref: ..., ...}, ...]}}

For example, imagine a user with id 123 online from two different devices, as well as a user with id 456 online from just one device. The following presence information might be returned:

%{"123" => %{metas: [%{status: "away", phx_ref: ...},
                     %{status: "online", phx_ref: ...}]},
  "456" => %{metas: [%{status: "online", phx_ref: ...}]}}

The keys of the map will usually point to a resource ID. The value will contain a map with a :metas key containing a list of metadata for each resource. Additionally, every metadata entry will contain a :phx_ref key which can be used to uniquely identify metadata for a given key. In the event that the metadata was previously updated, a :phx_ref_prev key will be present containing the previous :phx_ref value.

Link to this section Callbacks

Link to this callback handle_diff(%{}, state) View Source
handle_diff(%{optional(topic) => {joins :: presences, leaves :: presences}}, state :: term) :: {:ok, state :: term}
Link to this callback init(arg0) View Source
init(Keyword.t) :: {:ok, pid} | {:error, reason :: term}
Link to this callback start_link(arg0) View Source
start_link(Keyword.t) :: {:ok, pid} | {:error, reason :: term} :: :ignore
Link to this callback track(arg0, key, meta) View Source
track(Phoenix.Socket.t, key :: String.t, meta :: map) ::
  {:ok, binary} |
  {:error, reason :: term}
Link to this callback track(pid, topic, key, meta) View Source
track(pid, topic, key :: String.t, meta :: map) ::
  {:ok, binary} |
  {:error, reason :: term}
Link to this callback untrack(arg0, key) View Source
untrack(Phoenix.Socket.t, key :: String.t) :: :ok
Link to this callback untrack(pid, topic, key) View Source
untrack(pid, topic, key :: String.t) :: :ok
Link to this callback update(arg0, key, meta) View Source
update(Phoenix.Socket.t, key :: String.t, meta :: map | (map -> map)) ::
  {:ok, binary} |
  {:error, reason :: term}
Link to this callback update(pid, topic, key, meta) View Source
update(pid, topic, key :: String.t, meta :: map | (map -> map)) ::
  {:ok, binary} |
  {:error, reason :: term}