litestar-channels
Channels
Execution Workflow
- Choose the backend and durability model first: in-memory, Redis Pub/Sub, Redis Streams, or Postgres-backed variants.
- Define channel names or intentionally allow arbitrary channels.
- Publish events from domain boundaries through the injected
channelsplugin. - Manage subscriber lifecycle explicitly with
start_subscription(),subscribe(),unsubscribe(), and cleanup. - Decide whether history and backpressure need configuration before traffic arrives.
- Integrate with websocket handlers directly or generate websocket route handlers through the plugin.
Core Rules
- Treat
ChannelsPluginas the central routing and fanout component. - Keep event payloads compact, versionable, and transport-agnostic.
- Use the injected
channelsdependency instead of importing global plugin instances inside handlers. - Choose the backend based on inter-process needs, latency, and history requirements.
- Configure backpressure intentionally when slow subscribers are possible.
- Prefer
run_in_background()over manualiter_events()loops when concurrent websocket receive work is required. - Keep history replay bounded so subscribers do not drown in backlog immediately after connecting.
Decision Guide
- Use
MemoryChannelsBackendfor local development, tests, and single-process deployments. - Use
RedisChannelsPubSubBackendwhen low-latency fanout matters more than history. - Use
RedisChannelsStreamBackendwhen history replay is required. - Use generated websocket route handlers when each channel maps cleanly to a websocket subscription endpoint.
- Use manual websocket integration when authentication, multiplexing, or bidirectional behavior is more custom.
Reference Files
Read only the sections you need:
- For plugin configuration, publishing, subscription management, subscriber iteration, history replay, and direct websocket integration, read references/plugin-and-subscriber-patterns.md.
- For backend selection, backpressure strategy, and generated websocket route handlers, read references/backends-and-routing.md.
Recommended Defaults
- Define known channels up front unless arbitrary channel creation is part of the product.
- Publish from application services or domain events, not ad hoc transport code.
- Keep subscriber callbacks small and idempotent where retries or duplicate delivery are possible.
- Use bounded history and backlog settings when subscriber speed can vary materially.
- Reach for websocket integration only after the core event-stream model is sound.
Anti-Patterns
- Using channels when a single websocket connection with no fanout requirements would do.
- Publishing transport-specific payloads that make backend or consumer reuse hard.
- Ignoring backpressure in systems with bursty or slow consumers.
- Iterating
iter_events()beside a websocket receive loop without understanding disconnect risks. - Turning on arbitrary channels without a naming, authorization, or lifecycle strategy.
Validation Checklist
- Confirm backend choice matches latency, durability, and history needs.
- Confirm channel names and authorization expectations are explicit.
- Confirm publish paths work through the injected plugin.
- Confirm subscriber lifecycle is cleaned up on disconnect and shutdown.
- Confirm history replay and backlog settings cannot overwhelm new subscribers.
- Confirm websocket integration handles disconnects without hanging.
- Confirm generated websocket route handlers match the intended channel topology.
Cross-Skill Handoffs
- Use
litestar-websocketsfor client-facing websocket contract design and handler selection. - Use
litestar-testingfor subscriber, history, and websocket-fanout test coverage. - Use
litestar-metricsandlitestar-loggingfor throughput, lag, and delivery observability. - Use
litestar-dependency-injectionwhen publishers or subscriber callbacks need injected services.
Litestar References
More from alti3/litestar-skills
litestar-responses
Build Litestar responses with typed return values, explicit Response containers, layered response classes, headers, cookies, status-code control, redirects, files, streams, server-sent events, ASGI app returns, and background tasks. Use when shaping outbound HTTP behavior, correcting response contracts, or choosing the right Litestar response primitive. Do not use for request parsing, validation, or authentication policy design.
22litestar-routing
Design and implement Litestar routing with app/router/controller composition, handler decorators, path and parameter modeling, route indexing/reverse lookups, ASGI mounting, and layered route metadata. Use when creating or refactoring endpoint topology and URL contracts. Do not use for purely internal service logic unrelated to HTTP route structure.
18litestar-openapi
Configure Litestar OpenAPI schema generation and documentation UX with `OpenAPIConfig`, route-level schema metadata, schema generation controls, operation customization, UI render plugins, and documentation endpoint strategy. Use when API contract and docs quality must be implemented or corrected, especially for request, auth, and metrics surfaces. Do not use for runtime business logic changes unrelated to schema or documentation.
17litestar-databases
Build Litestar database architecture with SQLAlchemy and Piccolo ORM, including model/repository patterns, SQLAlchemy plugin selection, session lifecycle, transaction boundaries, and DTO/serialization controls. Use when implementing persistence layers, transaction handling, or ORM integration. Do not use for non-persistent in-memory workflows.
16litestar-debugging
Debug Litestar services with reproducible failure isolation, safe debug-mode usage, request and app logger inspection, middleware and dependency boundary analysis, and targeted regression checks. Use when investigating runtime errors, unexpected middleware behavior, lifecycle issues, request parsing failures, auth bugs, or route contract mismatches in Litestar. Do not use as a substitute for implementing missing tests, logging, metrics, or tracing instrumentation.
16litestar-plugins
Configure Litestar plugin architecture and ecosystem integrations, including custom Init/DI/serialization/route plugins, SQLAlchemy plugin stacks, Pydantic plugin surfaces, Piccolo DTO usage, and model-bound transport support for dataclasses, msgspec, attrs, and TypedDict. Use when selecting, wiring, or debugging plugin-driven behavior at application boundaries. Do not use for unrelated framework setup that does not involve Litestar plugin protocols, plugin registration, or plugin-backed transport/model integration.
16