Search documentation of phoenix_kit

# PhoenixKit.Admin.Presence (phoenix_kit v1.6.19)

View Source

Phoenix.Presence implementation for tracking anonymous and authenticated sessions.

This module provides real-time tracking of:

- Anonymous visitors (WebSocket connections without user_id)
- Authenticated users (with user_id)
- 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
    })

## Events Generated

- `{:anonymous_session_connected, session_id, session_info}`
- `{:anonymous_session_disconnected, session_id}`
- `{:user_session_connected, user_id, session_info}`
- `{:user_session_disconnected, user_id, session_id}`
- `{:presence_stats_updated, stats}`

# Summary

## Functions

child_spec(opts)
fetch(topic, presences)

Callback implementation for `Phoenix.Presence.fetch/2`.

fetchers_pids()
get_by_key(topic, key)

Callback implementation for `Phoenix.Presence.get_by_key/2`.

get_presence_stats()

Gets presence statistics.

get_topic()

Gets the presence topic name.

list(topic)

Callback implementation for `Phoenix.Presence.list/1`.

list_active_sessions()

Gets all currently active sessions (anonymous + authenticated).

list_anonymous_sessions()

Gets all anonymous sessions currently tracked.

list_authenticated_sessions()

Gets all authenticated user sessions currently tracked.

start_link(opts \\ [])

Starts the Presence process.

subscribe()

Subscribes to presence events for real-time updates.

track(socket, key, meta)

Callback implementation for `Phoenix.Presence.track/3`.

track(pid, topic, key, meta)

Callback implementation for `Phoenix.Presence.track/4`.

track_anonymous(session_id, metadata \\ %{})

Tracks an anonymous session in Presence.

track_user(user, metadata \\ %{})

Tracks an authenticated user session in Presence.

untrack(socket, key)

Callback implementation for `Phoenix.Presence.untrack/2`.

untrack(pid, topic, key)

Callback implementation for `Phoenix.Presence.untrack/3`.

update(socket, key, meta)

Callback implementation for `Phoenix.Presence.update/3`.

update(pid, topic, key, meta)

Callback implementation for `Phoenix.Presence.update/4`.

update_metadata(key, metadata_updates)

Updates metadata for an existing presence.

# Functions

# child_spec(opts)

# fetch(topic, presences)

Callback implementation for `Phoenix.Presence.fetch/2`.

# fetchers_pids()

# get_by_key(topic, key)

Callback implementation for `Phoenix.Presence.get_by_key/2`.

# get_presence_stats()

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

# get_topic()

Gets the presence topic name.

# list(topic)

Callback implementation for `Phoenix.Presence.list/1`.

# list_active_sessions()

Gets all currently active sessions (anonymous + authenticated).

Returns a list of session maps with type, metadata, and connection info.

# list_anonymous_sessions()

Gets all anonymous sessions currently tracked.

# list_authenticated_sessions()

Gets all authenticated user sessions currently tracked.

# start_link(opts \\ [])

Starts the Presence process.

# subscribe()

Subscribes to presence events for real-time updates.

# track(socket, key, meta)

Callback implementation for `Phoenix.Presence.track/3`.

# track(pid, topic, key, meta)

Callback implementation for `Phoenix.Presence.track/4`.

# track_anonymous(session_id, metadata \\ %{})

Tracks an anonymous session in Presence.

## Parameters

- `session_id` - Unique session identifier
- `metadata` - 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

# track_user(user, metadata \\ %{})

Tracks an authenticated user session in Presence.

## Parameters

- `user` - User struct with id and email
- `metadata` - 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

# untrack(socket, key)

Callback implementation for `Phoenix.Presence.untrack/2`.

# untrack(pid, topic, key)

Callback implementation for `Phoenix.Presence.untrack/3`.

# update(socket, key, meta)

Callback implementation for `Phoenix.Presence.update/3`.

# update(pid, topic, key, meta)

Callback implementation for `Phoenix.Presence.update/4`.

# update_metadata(key, metadata_updates)

Updates metadata for an existing presence.

Useful for updating current page or other session details.