Presence

Register process info on topic and replicate it across cluster.

  • Simple use case would be showing whose online

  • metadata should be minimized and used to store small, ephemeral state, such as a user's "online" or "away" status. Can fetch more substantial stuff by overriding the fetch/2 ft

Usage

Probably used in channel with access to socket

Presence.track(socket, socket.assigns.user_id, %{
      online_at: inspect(System.system_time(:second))
    });
push(socket, "presence_state", Presence.list(socket));

Presence.update(socket, socket.assigns.user_id, %{
	online_at: inspect(System.system_time(:second))
})
Presence.update(socket, socket.assigns.user_id, fn map -> Map.put(map, :name, name) end)
Presence.update( socket, socket.assigns.user_id,
&(Map.put(&1, :name, name) |> Map.put(:online_at, now)))

Presence.get_by_key(socket, socket.assigns.user_id)
MyPresence.get_by_key("room:1", "user1")

Complete Example

Need to have channel already setup

mix phx.gen.presence

Generates, ssweb/lib/channels/presence.ex

lib/hello/application.ex

lib/channels/user_socket.ex - add user_id to socket

lib/channels/room_channel.ex

Client

Actually (and before 1.4?) recieved "presence_diff" and "presence_state" events

Only one onSync handler allowed

More Client

Last updated