OctaStar.ServerSentEventGenerator (octa_star v0.2.0)

Copy Markdown

Plug-based Datastar Server-Sent Event generator.

This module is the Elixir equivalent of the SDK ADR's ServerSentEventGenerator namespace.

Summary

Functions

Checks whether a chunked SSE connection still accepts writes.

Sends a raw SSE event and raises on failure.

Starts an SSE response.

Functions

check_connection(conn)

@spec check_connection(Plug.Conn.t()) ::
  {:ok, Plug.Conn.t()} | {:error, Plug.Conn.t()}

Checks whether a chunked SSE connection still accepts writes.

format_event(event_type, data_lines, opts \\ [])

@spec format_event(String.t(), [String.t()] | String.t(), keyword()) :: String.t()

Formats a raw SSE event.

send(conn, event_type, data_lines, opts \\ [])

@spec send(Plug.Conn.t(), String.t(), [String.t()] | String.t(), keyword()) ::
  {:ok, Plug.Conn.t()} | {:error, term()}

Sends a raw SSE event.

send!(conn, event_type, data_lines, opts \\ [])

@spec send!(Plug.Conn.t(), String.t(), [String.t()] | String.t(), keyword()) ::
  Plug.Conn.t()

Sends a raw SSE event and raises on failure.

start(conn)

@spec start(Plug.Conn.t()) :: Plug.Conn.t()

Starts an SSE response.

It sets the required Datastar/SSE headers and starts a chunked 200 response. Connection: keep-alive is only set for HTTP/1.1 connections when the Plug adapter exposes the request protocol.