GenServer managing a single collaboration room session.
Started on-demand via DynamicSupervisor when the first user joins
(see PhiaUi.Collab.RoomManager.join_or_create_room/2).
Auto-terminates after an idle timeout (default 30 minutes) when all
users have left.
Behaviour Callbacks
Implement the behaviour in your own module to persist room state:
defmodule MyApp.CollabRoom do
@behaviour PhiaUi.Collab.CollabRoom
@impl true
def on_join(room_id, user), do: {:ok, %{}}
@impl true
def on_leave(room_id, user), do: :ok
@impl true
def load_room_state(room_id), do: {:ok, %{}}
@impl true
def save_room_state(room_id, state), do: :ok
endState Structure
The GenServer state is a %PhiaUi.Collab.CollabRoom{} struct containing:
:room_id— unique identifier for this room:callback_module— optional module implementing this behaviour:users— map ofuser_id => user_entryfor all joined users:custom_state— arbitrary map managed viaupdate_state/2:created_at/:last_activity_at— UTC timestamps:idle_timeout— milliseconds before an empty room shuts down
Summary
Callbacks
Load persisted room state on startup. Return {:ok, state_map} or {:error, reason}.
Called when a user joins the room. Return {:ok, meta} to attach metadata.
Called when a user leaves the room.
Persist room state after each update_state/2 call.
Functions
Check whether a room process is running.
Returns a specification to start this module under a supervisor.
Get the room's custom state.
Get all users currently in the room.
Join a user to the room. user must contain an :id key.
Remove a user from the room by user ID.
Start a CollabRoom process linked to the caller.
Refresh the room's last-activity timestamp to prevent idle shutdown.
Update the room's custom state by applying fun to the current state.
Callbacks
Load persisted room state on startup. Return {:ok, state_map} or {:error, reason}.
Called when a user joins the room. Return {:ok, meta} to attach metadata.
Called when a user leaves the room.
Persist room state after each update_state/2 call.
Functions
Check whether a room process is running.
Returns a specification to start this module under a supervisor.
See Supervisor.
Get the room's custom state.
Get all users currently in the room.
Join a user to the room. user must contain an :id key.
Remove a user from the room by user ID.
Start a CollabRoom process linked to the caller.
Options
:room_id(required) — unique room identifier:callback_module— module implementingCollabRoombehaviour:idle_timeout— milliseconds before empty room shuts down (default 30 min)
@spec touch(String.t()) :: :ok
Refresh the room's last-activity timestamp to prevent idle shutdown.
Update the room's custom state by applying fun to the current state.
The function receives the current custom state map and must return the new state map.
If a callback module with save_room_state/2 is configured, it is called after the update.