Server-Sent Events protocol implementation over ASGI.
Handles the full SSE lifecycle: sendstext/event-streamheaders,
produces events from an async generator, monitors for client disconnect,
and sends periodic heartbeat comments to keep the connection alive.
realtime.sse
| Name | Type | Default | Description |
|---|---|---|---|
type
|
|
— | |
qualified_name
|
|
— | |
element_type
|
|
— | |
description
|
|
— | |
source_file
|
|
— | |
line_number
|
|
— | |
is_autodoc
|
|
— | |
autodoc_element
|
|
— | |
_autodoc_template
|
|
— | |
_autodoc_url_path
|
|
— | |
_autodoc_page_type
|
|
— | |
title
|
|
— | |
doc_content_hash
|
|
— |
Symbols on this page
Return a selector-safe DOM id or fail with migration guidance.
Stream Server-Sent Events over an ASGI connection.
- Sends
http.response.startwithtext/event-streamheaders. - Launches two concurrent tasks:
- Event producer: consumes the async generator, converts…
Convert a yielded value to SSE wire format.
Dispatch:
SSEEvent-> encode as-isFragment-> render via kida, then use the selected client envelope…
Return bounded metadata about a formatted SSE event.
Format an error as an SSE event for a failed render.
For targetedFragmentvalues, htmx 2 uses the named event and htmx 4…
_validate_htmx4_sse_target
function
def _validate_htmx4_sse_target(target: str) -> str
Return a selector-safe DOM id or fail with migration guidance.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
target
|
str
|
— |
handle_sse
function
async
async def handle_sse(event_stream: EventStream, send: Send, receive: Receive, *, dialect: _SSEDialect = 'legacy', kida_env: Environment | None = None, debug: bool = False, retry_ms: int | None = None, close_event: str | None = None, allow_origin: str | None = None, vary_client_dialect: bool = False, trace_sink: Callable[[str, dict[str, Any]], None] | None = None, extra_headers: tuple[tuple[bytes, bytes], ...] = (), captured_context: _CapturedRequestContext | None = None) -> None
Stream Server-Sent Events over an ASGI connection.
- Sends
http.response.startwithtext/event-streamheaders. - Launches two concurrent tasks:
- Event producer: consumes the async generator, converts each yielded value to SSE wire format, and sends as ASGI body chunks.
- Disconnect monitor: awaits
http.disconnectfrom the client and cancels the producer.
- Sends periodic heartbeat comments (
:) on idle.
captured_context carries the request-scoped state (request, auth user,
CSRF token,g, CSP nonce) snapshotted at negotiation time, while the
middleware ContextVars were still live. The handlerfinallyresets
those vars the instant it returns — before any event is produced here — so
they are re-established insideproduce_events(in its own task) for the
lifetime of the stream. This keepsget_request(), get_user()/
current_user(), get_csrf_token(), g, and the live CSP nonce
working identically inside the EventStream generator (mirrors the
StreamingResponse drain in sender()).
SSE identity is pinned at connect time: the captured snapshot is fixed for the connection's lifetime. A user logged out or permission-revoked mid-stream keeps the connect-time identity until they reconnect.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
event_stream
|
EventStream
|
— | |
send
|
Send
|
— | |
receive
|
Receive
|
— | |
dialect
|
_SSEDialect
|
'legacy'
|
|
kida_env
|
Environment | None
|
None
|
|
debug
|
bool
|
False
|
|
retry_ms
|
int | None
|
None
|
|
close_event
|
str | None
|
None
|
|
allow_origin
|
str | None
|
None
|
|
vary_client_dialect
|
bool
|
False
|
|
trace_sink
|
Callable[[str, dict[str, Any]], None] | None
|
None
|
|
extra_headers
|
tuple[tuple[bytes, bytes], ...]
|
()
|
|
captured_context
|
_CapturedRequestContext | None
|
None
|
_format_event
function
def _format_event(value: Any, *, default_event: str | None = None, kida_env: Environment | None = None, dialect: _SSEDialect = 'legacy') -> str
Convert a yielded value to SSE wire format.
Dispatch:
SSEEvent-> encode as-isFragment-> render via kida, then use the selected client envelopestr-> wrap as datadict-> JSON-serialize as data
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
value
|
Any
|
— | |
default_event
|
str | None
|
None
|
|
kida_env
|
Environment | None
|
None
|
|
dialect
|
_SSEDialect
|
'legacy'
|
_trace_event_payload
function
def _trace_event_payload(value: Any, sse_text: str, *, dialect: _SSEDialect = 'legacy') -> dict[str, Any]
Return bounded metadata about a formatted SSE event.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
value
|
Any
|
— | |
sse_text
|
str
|
— | |
dialect
|
_SSEDialect
|
'legacy'
|
_format_error_event
function
def _format_error_event(value: Any, exc: Exception, *, dialect: _SSEDialect = 'legacy') -> str
Format an error as an SSE event for a failed render.
For targetedFragmentvalues, htmx 2 uses the named event and htmx 4
uses an unnamed partial so the error replaces the specific block in the
DOM. This keeps the broken block visible without changing render surfaces.
For other value types, sends a genericerrorevent.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
value
|
Any
|
— | |
exc
|
Exception
|
— | |
dialect
|
_SSEDialect
|
'legacy'
|
View source · /home/runner/work/chirp/chirp/site/../src/chirp/realtime/sse.py:1