DynamicSupervisor for managing multiple WhatsApp sessions.
This supervisor can start, stop, and manage multiple sessions dynamically. Each session is identified by a unique session ID and can be looked up via the built-in Registry.
Starting the Supervisor
Add to your application supervision tree:
children = [
ExWapp.Session.Supervisor
]
Supervisor.start_link(children, strategy: :one_for_one)Or start manually:
{:ok, _pid} = ExWapp.Session.Supervisor.start_link()Managing Sessions
# Start a new session
{:ok, session} = ExWapp.Session.Supervisor.start_session("user_123",
store: {ExWapp.Store.Ets, path: "/data/user_123.etf"}
)
# Look up an existing session
{:ok, session} = ExWapp.Session.Supervisor.get_session("user_123")
# List all sessions
sessions = ExWapp.Session.Supervisor.list_sessions()
#=> [{"user_123", #PID<0.234.0>}, {"user_456", #PID<0.267.0>}]
# Stop a session
:ok = ExWapp.Session.Supervisor.stop_session("user_123")Session IDs
Session IDs can be any term but strings are recommended. Common patterns:
- User ID:
"user_123" - Phone number:
"+1234567890" - UUID:
"550e8400-e29b-41d4-a716-446655440000"
Summary
Functions
Returns the child specification for use in a supervision tree.
Returns the count of active sessions.
Returns all live session pids matching a session ID.
Looks up a session by ID.
Returns a list of all active sessions.
Checks if a session exists.
Starts the session supervisor.
Starts a new session with the given ID.
Stops all live sessions for the given ID.
Stops a session by ID.
Functions
Returns the child specification for use in a supervision tree.
@spec count_sessions() :: non_neg_integer()
Returns the count of active sessions.
Returns all live session pids matching a session ID.
Looks up a session by ID.
Returns {:ok, pid} if found, or :error if not found.
Returns a list of all active sessions.
Returns a list of {session_id, pid} tuples.
Checks if a session exists.
@spec start_link(keyword()) :: Supervisor.on_start()
Starts the session supervisor.
This also starts the Registry for session lookup.
@spec start_session(term(), keyword() | ExWapp.Session.Options.t()) :: {:ok, pid()} | {:error, term()}
Starts a new session with the given ID.
Returns {:ok, pid} if successful, or {:error, reason} if the session
could not be started or already exists.
Options
:store- Storage adapter (seeExWapp.Store):auto_connect- Connect automatically on start (default: false):reconnect- Enable automatic reconnection (default: true):runtime- Runtime hardening config override for this session
Examples
{:ok, session} = ExWapp.Session.Supervisor.start_session("user_123",
store: {ExWapp.Store.Ets, path: "/data/sessions/user_123.etf"}
)
# With auto-connect
{:ok, session} = ExWapp.Session.Supervisor.start_session("user_456",
store: ExWapp.Store.Memory,
auto_connect: true
)
@spec stop_all_sessions(term()) :: :ok
Stops all live sessions for the given ID.
@spec stop_session(term()) :: :ok | {:error, :not_found}
Stops a session by ID.
Returns :ok if the session was stopped, or {:error, :not_found} if
no session with that ID exists.