PhiaUi.Collab.RoomManager (phia_ui v0.1.17)

Copy Markdown View Source

Manages collaboration room lifecycles via DynamicSupervisor.

Provides join-or-create semantics: if a room already exists, returns its PID; otherwise starts a new PhiaUi.Collab.CollabRoom GenServer under the PhiaUi.Collab.RoomSupervisor.

This module is stateless — it delegates to the RoomRegistry (a Registry) for lookups and to the RoomSupervisor (a DynamicSupervisor) for process management. Both are started by PhiaUi.Collab.Supervisor.

Example

# Ensure the room exists (idempotent)
{:ok, pid} = PhiaUi.Collab.RoomManager.join_or_create_room("doc-123",
  callback_module: MyApp.CollabCallbacks,
  idle_timeout: :timer.minutes(15)
)

# Later, when user disconnects
PhiaUi.Collab.RoomManager.leave_room("doc-123", "user-42")

Summary

Functions

Get the PID of a running room, or nil if not running.

Join or create a collaboration room.

Leave a room and trigger cleanup if empty.

List all active room IDs.

Count the number of currently active rooms.

Functions

get_room(room_id)

@spec get_room(String.t()) :: pid() | nil

Get the PID of a running room, or nil if not running.

join_or_create_room(room_id, opts \\ [])

@spec join_or_create_room(
  String.t(),
  keyword()
) :: {:ok, pid()} | {:error, term()}

Join or create a collaboration room.

If the room exists, returns {:ok, pid}. Otherwise starts a new CollabRoom GenServer under the DynamicSupervisor.

Options

  • :callback_module — module implementing PhiaUi.Collab.CollabRoom behaviour
  • :idle_timeout — milliseconds before an empty room shuts down (default 30 min)

Returns

  • {:ok, pid} — the room process PID
  • {:error, reason} — if the room could not be started

leave_room(room_id, user_id)

@spec leave_room(String.t(), String.t()) :: :ok

Leave a room and trigger cleanup if empty.

Safe to call even if the room is not running — returns :ok in all cases.

list_rooms()

@spec list_rooms() :: [String.t()]

List all active room IDs.

Returns a list of room ID strings for all currently running CollabRoom processes.

room_count()

@spec room_count() :: non_neg_integer()

Count the number of currently active rooms.