Realtime & AsyncAPI#
Litestar supports WebSockets, event broadcasting via ChannelsPlugin, and Server-Sent Events (SSE).
litestar-vite connects these server-side endpoints with frontend code:
AsyncAPI Export: Exports the registered
AsyncAPIPlugindocument asasyncapi.json.TypeScript Channel Contracts: Generates channel definitions (
channels.ts) for frontend clients.Client Streams & Composables: Connects to channels and event feeds with typed React hooks, Vue composables, and Svelte stores.
Build full-duplex interactive WebSocket endpoints using @websocket and websocket_listener.
Publish and subscribe across channels using ChannelsPlugin with Memory, Redis, or Postgres backends.
Stream lightweight unidirectional event feeds to web clients using ServerSentEvent handlers.
Export AsyncAPI specifications and emit type-safe TypeScript channel interfaces.
Bind generated channel contracts to browser streams and link onward to the full helper reference.
Integrate real-time streams with React (hooks), Vue 3 (composables), and Svelte 5 (stores).
Mental Model#
Realtime in litestar-vite operates on a unified contract pipeline:
flowchart LR
subgraph Backend ["Litestar Backend"]
WS["@websocket / listener"]
CH["ChannelsPlugin"]
SSE["ServerSentEvent"]
end
subgraph Pipeline ["Contract Pipeline"]
GEN["Registered AsyncAPIPlugin"]
SCHEMA["asyncapi.json"]
TS["channels.ts"]
end
subgraph Frontend ["Frontend Consumer"]
HELPERS["createChannelsStream"]
UI["React / Vue / Svelte"]
end
WS --> GEN
CH --> GEN
SSE --> GEN
GEN --> SCHEMA
SCHEMA --> TS
TS --> HELPERS
HELPERS --> UI
Schema ownership: Configure realtime contracts in
litestar-asyncapiand register itsAsyncAPIPlugin.Schema Emission: With channel generation enabled,
litestar assets build(orgenerate-types) exports that plugin’s document asasyncapi.json. Without the plugin, no realtime document is exported.TypeScript Generation: TypeGen emits strongly typed TypeScript contracts (
channels.ts) directly into your frontend sources.Client Consumption: Browser helpers and UI framework composables bind directly to the emitted types.