infrastructure.sse.redis_publisher¶
src.infrastructure.sse.redis_publisher
¶
Redis SSE Publisher implementing SSEPublisherProtocol.
This adapter publishes SSE events via Redis pub/sub for real-time delivery to connected clients. Optionally stores events in Redis Streams for Last-Event-ID replay on reconnection.
Architecture
- Implements SSEPublisherProtocol without inheritance (structural typing)
- Uses Redis pub/sub for horizontal scaling (multiple API instances)
- Optional Redis Streams for event retention
- Fail-open design: publish errors are logged but don't raise
Reference
- docs/architecture/sse-architecture.md
Attributes¶
Classes¶
RedisSSEPublisher
¶
Redis implementation of SSEPublisherProtocol.
Publishes SSE events to user channels via Redis pub/sub. When retention is enabled, also stores in Redis Streams for replay.
Note: Does NOT inherit from SSEPublisherProtocol (uses structural typing).
Attributes:
| Name | Type | Description |
|---|---|---|
_redis |
Async Redis client instance. |
|
_enable_retention |
Whether to store events in Redis Streams. |
|
_retention_max_len |
Max events to retain per user. |
|
_retention_ttl |
TTL for retained events (seconds). |
|
_logger |
Logger instance. |
Source code in src/infrastructure/sse/redis_publisher.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | |
Functions¶
__init__
¶
__init__(
redis_client: Redis[bytes],
enable_retention: bool = False,
retention_max_len: int = SSE_RETENTION_MAX_LEN_DEFAULT,
retention_ttl_seconds: int = SSE_RETENTION_TTL_DEFAULT,
logger: Logger | None = None,
) -> None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
redis_client
|
Redis[bytes]
|
Async Redis client instance. |
required |
enable_retention
|
bool
|
Store events in Redis Streams for replay. |
False
|
retention_max_len
|
int
|
Max events per user stream (MAXLEN). |
SSE_RETENTION_MAX_LEN_DEFAULT
|
retention_ttl_seconds
|
int
|
TTL for stream entries. |
SSE_RETENTION_TTL_DEFAULT
|
logger
|
Logger | None
|
Optional logger (creates default if not provided). |
None
|
Source code in src/infrastructure/sse/redis_publisher.py
publish
async
¶
Publish SSE event to user's channel.
Routes event to the user's Redis pub/sub channel. If retention is enabled, also stores in Redis Stream for replay.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
SSEEvent
|
SSE event to publish. Contains user_id for routing. |
required |
Note
- Fail-open: Errors are logged but not raised
- Non-blocking: Returns immediately after publish
Source code in src/infrastructure/sse/redis_publisher.py
publish_to_user
async
¶
Publish SSE event to specific user's channel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
Target user ID. |
required |
event
|
SSEEvent
|
SSE event to publish. |
required |
Note
Same fail-open behavior as publish().
Source code in src/infrastructure/sse/redis_publisher.py
broadcast
async
¶
Broadcast SSE event to all connected clients.
Publishes to the global broadcast channel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
SSEEvent
|
SSE event to broadcast. |
required |
Note
Event's user_id is preserved but event goes to all users.