SSETarget
SSETarget connects a DOM container to an SSE endpoint and updates it as events arrive.
Quick Start
Swap Modes
Valid values:
innerouterbeforeafterappendprependreplacetext
Use text if you want to update plain text only.
Targeting Another Element
Connection Options
SSETarget(
"Live",
endpoint="/api/stream",
event="message",
with_credentials=True,
reconnect=True,
retry=3000,
)
Use retry to suggest a reconnection delay (milliseconds).
For swap="outer" or swap="replace", stream a single root element so the target can be rebound safely after replacement.
Pair With SSEStream
from faststrap.presets import SSEStream, sse_event
@app.get("/api/stream")
async def stream():
async def gen():
yield sse_event("Hello")
return SSEStream(gen())
Accessibility
SSETarget sets aria-live="polite" by default. Set aria_live=None to disable the live region.
Security Notes
Protect your SSE endpoints with auth and rate limits. Do not stream sensitive data to unauthenticated clients. If you stream HTML fragments, treat them as trusted server-rendered content.
API Reference
faststrap.components.display.sse_target.SSETarget(*children, endpoint, event='message', swap='inner', target=None, with_credentials=False, reconnect=True, retry=None, aria_live='polite', content=None, **kwargs)
Client-side SSE target that updates when new events arrive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*children
|
Any
|
Initial content to render inside the target. |
()
|
endpoint
|
str
|
SSE endpoint URL (must return text/event-stream). |
required |
event
|
str
|
SSE event name to listen for (default: "message"). |
'message'
|
swap
|
SSESwapType
|
How to apply incoming data ("inner", "outer", "append", "text", etc.). |
'inner'
|
target
|
str | None
|
Optional CSS selector for a separate element to update. |
None
|
with_credentials
|
bool
|
Use cookies/credentials with EventSource. |
False
|
reconnect
|
bool
|
Whether to allow automatic reconnect (default: True). |
True
|
retry
|
int | None
|
Optional reconnect delay hint (milliseconds). |
None
|
aria_live
|
str | None
|
ARIA live region value (default: "polite"). |
'polite'
|
content
|
Any | None
|
Optional alternative to *children for initial content. |
None
|
**kwargs
|
Any
|
Additional HTML attributes. |
{}
|