GenServer for live PhoenixDatastar views.
Manages the socket state and handles:
- Event dispatching from client
- PubSub message forwarding to view's handle_info/2
- SSE updates to subscribers
- Cleanup on disconnect via terminate/1
Summary
Functions
Returns a specification to start this module under a supervisor.
Dispatches an event to the GenServer for handling.
Ensures a GenServer is started for the given session.
Enters the SSE loop, receiving updates from the GenServer and sending them to the client. Takes a %PhoenixDatastar.SSE{} struct instead of a raw conn.
Get the current rendered HTML and initial signals without subscribing.
Replaces the active view/socket state for an existing live session and pushes the new content through the SSE stream.
Starts a GenServer for a Datastar view session.
Subscribe the calling process to receive render updates. Does not return initial HTML (use get_snapshot/1 if needed).
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
Dispatches an event to the GenServer for handling.
This is an async cast - the response is sent via the SSE stream.
@spec ensure_started(module(), String.t(), map(), map(), String.t()) :: {:ok, pid()} | {:error, term()}
Ensures a GenServer is started for the given session.
If already running, returns the existing pid. Idempotent.
@spec enter_loop(PhoenixDatastar.SSE.t(), String.t()) :: PhoenixDatastar.SSE.t()
Enters the SSE loop, receiving updates from the GenServer and sending them to the client. Takes a %PhoenixDatastar.SSE{} struct instead of a raw conn.
Returns the SSE struct when the connection closes.
Get the current rendered HTML and initial signals without subscribing.
@spec start_link(keyword()) :: GenServer.on_start()
Starts a GenServer for a Datastar view session.
Options
:view- Required. The view module.:session_id- Required. Unique session identifier.:params- Route params. Defaults to%{}.:session- Plug session data. Defaults to%{}.:base_path- Base path for event URLs. Defaults to"".
@spec subscribe(String.t()) :: :ok
Subscribe the calling process to receive render updates. Does not return initial HTML (use get_snapshot/1 if needed).