Implement Redis-backed WebSocket Pub/Sub helpers#32
Open
emiliano-go wants to merge 6 commits into
Open
Conversation
PubSubManager with subscribe/listen/publish/close methods. FastAPI DI via get_pubsub_manager (sub-dependency on Depends(get_async_redis) for proper override chains). record_pubsub_publish OpenTelemetry counter. Each subscribe() call consumes one dedicated pool connection (inherent Redis Pub/Sub limitation).
Core lifecycle, 13 edge cases (empty channels, unicode, binary-safe, burst, concurrent subscribers, 1MB payload), memory leak via weakref, DI integration, and error handling.
Cross-connection bridge, subscriber count, and unsub isolation. Requires a real Redis server.
Guide with WebSocket examples, connection pool limitation note, and DI override chains. API reference section with PubSubManagerDep, get_pubsub_manager, and PubSubManager.
/notify HTTP endpoint for publishing messages to Redis channels.
/ws/chat/{room} WebSocket endpoint for bidirectional chat
via Redis Pub/Sub broadcast.
get_async_redis now takes starlette.requests.HTTPConnection instead of fastapi.Request, so it works in both HTTP endpoints and WebSocket handlers. PubSubManagerDep and PubSubManager can now be injected directly in WebSocket endpoints without manually calling _get_pool_state. This approach was inspired by PR redis#27 (massooti).
Author
|
I just noticed PR #27 after opening this one (started working yesterday at night, too tired). The two PRs target the same feature but go about it differently. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implement Redis-backed WebSocket Pub/Sub helpers (Issue #24)
5 commits:
feat: add PubSubManager for Redis Pub/Sub messaging
pubsub.py,__init__.py,deps.py,telemetry.pyPubSubManager with subscribe/listen/publish/close. FastAPI DI via
get_pubsub_manager(sub-dependency onDepends(get_async_redis)).record_pubsub_publishtelemetry counter.test: add 25 PubSub unit tests
tests/unit/test_pubsub.pyCore lifecycle, 13 edge cases, memory leak via weakref, DI integration.
test: add PubSub integration tests
tests/integration/test_pubsub.pyCross-connection bridge, subscriber count, unsub isolation (requires real Redis).
docs: add PubSub user guide and API reference
docs/guide/pubsub.md,docs/api/reference.md,mkdocs.ymlGuide with WebSocket examples, connection pool limitation note, DI override chains.
examples: add PubSub WebSocket chat and notify endpoint
examples/main.py/notifyHTTP publish and/ws/chat/{room}WebSocket broadcast loop.Design decisions
get_pubsub_managerusesDepends(get_async_redis)instead of callingget_async_redisdirectly, so callers can override the Redis client without re-registering the PubSub provider.listen()filters system events and yields onlymessage-type events.subscribe()call consumes one dedicated pool connection (inherent Redis Pub/Sub limitation), documented in the guide.Closes #24