Standard Events — Aquilia Documentation
Comprehensive guide and documentation for Standard Events in the Aquilia framework. View API reference, examples, and implementation patterns.
SSE SYSTEM / STANDARD EVENTS Standard Events & Spec Understand the structure of SSEEvent , custom event namespaces, retry options, and client reconnection behaviors. SSEEvent Structure An SSEEvent represents a structured data payload formatted strictly according to the W3C Server-Sent Events specification. The properties of the class translate directly to fields in the event stream: class SSEEvent: data: str # The raw data payload. Splits multi-line strings automatically. id: str | None = None # Event identifier. Used by browsers to query missing events. event: str | None = None # Event name tag. Used on client to filter events. retry: int | None = None # Reconnect retry delay in milliseconds. Custom Event Filters By default, events pushed through an SSE stream have no event name, and are caught by the browser's generic onmessage handler. Specifying the event string parameter allows you to partition streams. The browser will then trigger specific event listeners registered for that name. import asyncio from aquilia.controller import Controller, GET, RequestCtx from aquilia.sse import SSEResponse, SSEEvent class FeedController(Controller): @GET("/sse/news") async def news_feed(self, ctx: RequestCtx): return SSEResponse(self._generate_feed()) async def _generate_feed(self): # Push to 'sports' listener yield SSEEvent(data="Local match won 3-1", event="sports") await asyncio.sleep(0.5) # Push to 'weather' listener yield SSEEvent(data="Heavy rain warning", event="weather") Reconnections & Caching (Last-Event-ID) When a connection drops (due to network changes or server restarts), the browser's `EventSource` client attempts to reconnect automatically. To prevent data loss, the client sends the last received event ID in the Last-Event-ID header. The server can read this header and stream missing logs starting from that ID. Using the retry parameter, the server can dynamically change the browser's reconnect cooldown interval. Consuming on the Client (JavaScript) Register event listeners on the browser client for custom namespaces: const eventSource = new EventSource('/sse/news'); // 1. Listen to the generic stream (unnamed events) eventSource.onmessage = (event) => ; // 2. Listen to custom namespace updates eventSource.addEventListener('sports', (event) => ); eventSource.addEventListener('weather', (event) => ); // 3. Handle connection errors eventSource.onerror = (err) => ; Overview Text & JSON Streams )
Go to Homepage