Server-Sent Events
Server-Sent Events (SSE) push a continuous stream of text events from your server over standard HTTP. use_event_source() returns an EventSourceHandle that behaves as an AsyncIterator[SSEvent], shares connections cleanly across components in the same app, supports both browser-native EventSource (GET) and streaming fetch requests (POST), and degrades safely during SSR and static generation.
When to use#
- Need server-to-client push with automatic reconnect and named events — use
use_event_source(). - Need bidirectional client-to-server messaging as well — use WebSocket (or Typed Realtime).
- Running a finite, single-request typed RPC generator (such as LLM token generation for one prompt) — use RPC Streaming.
- Performing a one-shot JSON fetch — use
HttpClientanduse_async_result.
Setup#
Call use_event_source(url) in component setup and bridge it with to_reactive_list (or iterate in @on_mounted):
from webcompy import ConnectionState, use_computed
from webcompy.aio import to_reactive_list
from webcompy.components import ComponentContext, define_component
from webcompy.elements import html, repeat
from webcompy.realtime import use_event_source
@define_component()
def EventFeed(context: ComponentContext[None]):
es = use_event_source("/api/events")
feed = to_reactive_list(es, maxlen=50)
return html.DIV(
{},
html.P({}, use_computed(lambda: f"State: {es.state.value.value}")),
html.UL(
{},
repeat(feed.items, lambda ev: html.LI({}, ev.data)),
),
)
Each yielded SSEvent is a frozen dataclass with three string attributes: event (defaults to "message"), data, and last_event_id.
Recipes#
Subscribe to named event types and track connection state#
Pass events=(...) as a non-empty tuple of event names and observe es.state.value against ConnectionState (CONNECTING, OPEN, RECONNECTING, CLOSED):
from webcompy import ConnectionState, use_computed
from webcompy.components import ComponentContext, define_component, on_mounted
from webcompy.elements import html
from webcompy.realtime import use_event_source
from webcompy.signal import use_state
@define_component()
def DeployMonitor(context: ComponentContext[None]):
es = use_event_source("/api/deploy/stream", events=("progress", "done"))
status = use_state(lambda: "Waiting")
@on_mounted
async def read_events():
async for ev in es:
if ev.event == "progress":
status.value = f"Step: {ev.data}"
elif ev.event == "done":
status.value = f"Finished ({ev.data}) [id={ev.last_event_id}]"
es.close()
return html.DIV(
{},
html.SPAN(
{},
use_computed(lambda: "Live" if es.state.value == ConnectionState.OPEN else es.state.value.value),
),
html.P({}, status),
)
Share one connection across components#
Inside the same app DI scope, multiple use_event_source("/api/events") calls share a single underlying connection. Each caller gets its own EventSourceHandle and independent FIFO queue (max_queue), so a slow consumer never blocks another:
# In StatusWidget
status_es = use_event_source("/api/stream", events=("status",), max_queue=32)
# In MetricsWidget mounted in the same app
metrics_es = use_event_source("/api/stream", events=("metrics",))
For GET connections, when a second subscriber requests an event name not yet subscribed ("metrics"), the shared browser connection automatically reopens listening to the union ("status", "metrics"). Calling status_es.close() detaches only status_es and keeps the shared socket open until metrics_es also closes or unmounts.
Stream POST SSE endpoints (e.g., AI prompts or filtered queries)#
Pass method="POST" along with body= and headers= to use WebComPy's streaming FetchPort transport instead of the browser's GET-only EventSource API:
import json
from webcompy.aio import to_reactive_list
from webcompy.components import ComponentContext, define_component
from webcompy.elements import html, repeat
from webcompy.realtime import use_event_source
@define_component()
def SummaryStream(context: ComponentContext[None]):
es = use_event_source(
"/api/summarize",
method="POST",
headers={"Content-Type": "application/json"},
body=json.dumps({"doc_id": 42}),
events=("token", "done"),
)
tokens = to_reactive_list(es)
return html.DIV(
{},
repeat(tokens.items, lambda ev: html.SPAN({}, ev.data)),
)
Fetch-based SSE connections are keyed in the shared registry by (url, method, body, normalized_headers) (with lowercased header names) and filter events per subscriber on the client without reopening the request when another subscriber asks for another event type.
Behavior notes#
- Reconnection and
Last-Event-ID:- With
method="GET", native browserEventSourcemanages automatic reconnection and sends theLast-Event-IDheader automatically after a drop. - With non-
GETmethods (e.g.POST), WebComPy reconnects automatically using exponential backoff with random jitter (min(30.0, 1.0 * 2 ** (attempt - 1)) * random.uniform(0.5, 1.0)), carrying the most recent non-emptyid:field in theLast-Event-IDheader.
- With
- Lifecycle cleanup: When called inside component setup,
use_event_source()registers a chainedon_before_destroyhook callinghandle.close(). Only the last remaining subscriber closes the underlying browser connection. - Testing & standalone usage: Outside a component context,
.close()can still be called explicitly, and aweakref.finalizefallback detaches abandoned handles on GC. When called outside an active app DI scope,use_event_source()warns and opens an unshared connection. - SSR and hydration: On the server and during
webcompy generate,ServerEventSourcePort(forGET) andServerFetchPortreturn an immediately exhausted handle withstate.value == ConnectionState.CLOSED, emit a warning, and transfer nothing into<script id="__webcompy_data__">.
Pitfalls#
- Passing
bodyorheaderswithmethod="GET":use_event_source()raisesValueErrorimmediately ifbodyorheadersis supplied alongsidemethod="GET", because nativeEventSourcecannot carry custom headers or a request body. - Passing a bare string for
events=:events="message"raisesTypeError— always pass a tuple such asevents=("message",). - Expecting
Last-Event-IDreplay without server support: Reconnection only replays missed events if your server assignsid:fields on emitted SSE frames and honors the incomingLast-Event-IDheader. - Proxy buffering stalling delivery: Reverse proxies and CDNs often buffer small HTTP chunks until compressed or flushed. Serve SSE endpoints with
Content-Type: text/event-stream, disable compression on that route, and includeX-Accel-Buffering: nowhere needed. - Closing a handle cancels only your subscription: Calling
.close()inside one component does not force-close the server stream if another component still holds a subscription to the same shared connection.
Next steps#
- Use full-duplex text channels in WebSocket or schema-validated frames in Typed Realtime.
- Convert SSE streams into UI state in Signals and Streams.
- Stream finite, request-scoped generator outputs via RPC Streaming.
- Review basic request/response fetching in HTTP & Async.
- Full API: Data, Forms & i18n API.