Phoenix.Presence implementation for tracking anonymous and authenticated sessions.
This module provides real-time tracking of:
- Anonymous visitors (WebSocket connections without user_uuid)
- Authenticated users (with user_uuid)
- Session details like IP, User-Agent, current page, connection time
Usage
Track anonymous session:
PhoenixKit.Admin.Presence.track_anonymous(session_id, %{
connected_at: DateTime.utc_now(),
ip_address: get_connect_info(socket, :peer_data).address,
user_agent: get_connect_info(socket, :user_agent),
current_page: socket.assigns.current_path
})Track authenticated session:
PhoenixKit.Admin.Presence.track_user(user, %{
connected_at: DateTime.utc_now(),
session_id: session_id,
ip_address: get_connect_info(socket, :peer_data).address
})track_user/2 is idempotent by (user.uuid, metadata.session_id) — tracking
the same user/session pair again (a second open tab, another LiveView
mounted under the same login) merges into the existing presence row instead
of creating a duplicate. See PhoenixKit.Admin.SimplePresence for the
multi-tab monitor-refcounting this relies on.
Events Generated
{:anonymous_session_connected, session_id, session_info}{:anonymous_session_disconnected, session_id}{:user_session_connected, user_uuid, session_info}{:user_session_disconnected, user_uuid, session_id}{:presence_stats_updated, stats}
Summary
Functions
Callback implementation for Phoenix.Presence.fetch/2.
Callback implementation for Phoenix.Presence.get_by_key/2.
Gets presence statistics.
Gets the presence topic name.
Callback implementation for Phoenix.Presence.list/1.
Gets all currently active sessions (anonymous + authenticated).
Gets all anonymous sessions currently tracked.
Gets all authenticated user sessions currently tracked.
Starts the Presence process.
Subscribes to presence events for real-time updates.
Callback implementation for Phoenix.Presence.track/3.
Callback implementation for Phoenix.Presence.track/4.
Tracks an anonymous session in Presence.
Tracks an authenticated user session in Presence.
Callback implementation for Phoenix.Presence.untrack/2.
Callback implementation for Phoenix.Presence.untrack/3.
Callback implementation for Phoenix.Presence.update/3.
Callback implementation for Phoenix.Presence.update/4.
Updates the :current_page an authenticated user's tracked session reports.
Updates metadata for an existing presence.
Builds the presence key for a user's session — see SimplePresence.user_key/2.
Functions
Callback implementation for Phoenix.Presence.fetch/2.
Callback implementation for Phoenix.Presence.get_by_key/2.
Gets presence statistics.
Returns statistics about current active sessions:
- Total sessions
- Anonymous sessions count
- Authenticated sessions count
- Unique anonymous visitors
- Active authenticated users
- Top pages by activity
Gets the presence topic name.
Callback implementation for Phoenix.Presence.list/1.
Gets all currently active sessions (anonymous + authenticated).
Returns a list of session maps with type, metadata, and connection info.
Gets all anonymous sessions currently tracked.
Gets all authenticated user sessions currently tracked.
Starts the Presence process.
Subscribes to presence events for real-time updates.
Callback implementation for Phoenix.Presence.track/3.
Callback implementation for Phoenix.Presence.track/4.
Tracks an anonymous session in Presence.
Parameters
session_id- Unique session identifiermetadata- Session metadata map
Metadata Fields
:connected_at- Connection timestamp:ip_address- Client IP address:user_agent- Browser User-Agent string:current_page- Current page path
Tracks an authenticated user session in Presence.
Parameters
user- User struct with id and emailmetadata- Session metadata map
Metadata Fields
:connected_at- Connection timestamp:session_id- Session identifier:ip_address- Client IP address:user_agent- Browser User-Agent string:current_page- Current page path
Callback implementation for Phoenix.Presence.untrack/2.
Callback implementation for Phoenix.Presence.untrack/3.
Callback implementation for Phoenix.Presence.update/3.
Callback implementation for Phoenix.Presence.update/4.
Updates the :current_page an authenticated user's tracked session reports.
A no-op on any failure — the session no longer being tracked (a race between
navigation and disconnection) or SimplePresence not running at all (see
SimplePresence's catch :exit on the same gap) must not raise and take an
in-flight navigation down with it.
Updates metadata for an existing presence.
Useful for updating current page or other session details.
Builds the presence key for a user's session — see SimplePresence.user_key/2.