litestar-realtime

Implement Litestar WebSockets, SSE, and Channels pub/sub with typed realtime event contracts.

Updated Aug 17, 2026
One-click install
npx skills add https://github.com/renjianguo666/litecms --skill litestar-realtime-renjianguo666
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: litestar-realtime
Source: https://github.com/renjianguo666/litecms/tree/main/.agents/skills/litestar-realtime
Command: npx skills add https://github.com/renjianguo666/litecms --skill litestar-realtime-renjianguo666

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires litestar, msgspec, redis, saq, dishka, sqlspec, and includes references (resource) components.

What problem does it solve? Building realtime features in Litestar requires choosing between plain WebSockets, SSE, and the Channels plugin, wiring authentication that works without HTTP headers, and publishing typed events across processes from workers or scripts. This Skill provides the patterns, guardrails, and reference implementations for that entire realtime stack. ## Core Features & Use Cases - Transport Selection Guidance: Decision matrix for plain WebSocket handlers versus the Channels plugin, plus backend selection across Memory, Redis, sqlspec PostgreSQL LISTEN/NOTIFY, and advanced-alchemy backends. - Typed Event Contracts: Canonical RealtimeEvent envelope with scope-based ACLs (workspace, user, global), channel naming factories, and a RealtimePublisher abstraction with graceful no-op when the backend is uninitialized. - WebSocket Auth & Lifecycle: Query-param JWT authentication guards, multi-tenant membership checks, Dishka request-scope handling, and a stream_pubsub subscriber with idempotency deduplication and per-session metrics. - Use Case: A SAQ background worker finishes an ETL job and needs to notify connected browsers. Use this Skill to publish a typed workspace-scoped event through the shared Channels backend so subscribed WebSocket clients receive the update in real time. ## Quick Start Ask the assistant to implement a Litestar WebSocket endpoint that streams workspace events using the Channels plugin with token-based authentication.

Frequently Asked Questions about litestar-realtime

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement WebSocket handlers in Litestar?

Use the @websocket decorator on a handler that receives a WebSocket object, call socket.accept(), then loop over receive_json and send_json. Catch WebSocketDisconnect silently since client disconnects are normal, and set opt={"exclude_from_csrf": True, "exclude_from_auth": True} to bypass HTTP middleware.

When should I use Litestar Channels plugin vs plain WebSockets?

Use plain WebSockets for one-off streams with few channel names. Use the Channels plugin when you need dynamic topics, message history, automatic /ws/{channel} handlers, or cross-process publishing from workers and scripts through a shared backend.

How do I authenticate WebSocket connections in Litestar?

Browsers cannot set arbitrary headers during the WebSocket handshake, so pass a JWT as a token query parameter. Write a guard that decodes the token, loads the user, attaches it to connection.state, and raises WebSocketException with code 4001 on failure.

Which Channels backend should I use with PostgreSQL?

For sqlspec plus PostgreSQL stacks, use the SQLSpecChannelsBackend which reuses the existing connection pool via LISTEN/NOTIFY. Avoid adding Redis solely for Channels in a PostgreSQL-only project, and avoid PG backends when Redis already handles queues and caching.

How do I publish events to WebSocket clients from a background worker?

Import the same ChannelsPlugin instance in your SAQ worker and call channels.wait_published with the channel name and payload. Since the backend is shared across processes, any worker or script can publish while only subscribed WebSocket clients receive messages.

Why does my Litestar realtime publish silently fail during startup?

The Channels backend may not be initialized before the Litestar lifespan runs, such as in early-starting background workers. The RealtimePublisher pattern catches this RuntimeError and skips the publish with a debug log instead of raising.