PhoenixDatastar.Server (PhoenixDatastar v0.1.14)

Copy Markdown View Source

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

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

dispatch_event(session_id, event, payload)

@spec dispatch_event(String.t(), String.t(), map()) :: :ok

Dispatches an event to the GenServer for handling.

This is an async cast - the response is sent via the SSE stream.

ensure_started(view, session_id, params, session, base_path \\ "")

@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.

enter_loop(sse, session_id)

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_snapshot(session_id)

@spec get_snapshot(String.t()) :: {:ok, String.t(), map()}

Get the current rendered HTML and initial signals without subscribing.

start_link(opts)

@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 "".

subscribe(session_id)

@spec subscribe(String.t()) :: :ok

Subscribe the calling process to receive render updates. Does not return initial HTML (use get_snapshot/1 if needed).