infrastructure.sse.redis_subscriber¶
src.infrastructure.sse.redis_subscriber
¶
Redis SSE Subscriber implementing SSESubscriberProtocol.
This adapter subscribes to SSE events via Redis pub/sub and yields them as an async generator. Supports category filtering and Last-Event-ID replay from Redis Streams.
Architecture
- Implements SSESubscriberProtocol without inheritance (structural typing)
- Uses Redis pub/sub for real-time event delivery
- Async generator pattern for streaming
- Optional Redis Streams for missed event replay
Reference
- docs/architecture/sse-architecture.md
Classes¶
RedisSSESubscriber
¶
Redis implementation of SSESubscriberProtocol.
Subscribes to user and broadcast channels via Redis pub/sub, yielding events as they arrive. Supports category filtering and replay of missed events from Redis Streams.
Note: Does NOT inherit from SSESubscriberProtocol (uses structural typing).
Attributes:
| Name | Type | Description |
|---|---|---|
_redis |
Async Redis client instance. |
|
_enable_retention |
Whether retention is available for replay. |
|
_logger |
Logger instance. |
Source code in src/infrastructure/sse/redis_subscriber.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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
Functions¶
__init__
¶
__init__(
redis_client: Redis[bytes],
enable_retention: bool = False,
logger: Logger | None = None,
) -> None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
redis_client
|
Redis[bytes]
|
Async Redis client instance. |
required |
enable_retention
|
bool
|
Whether retention is enabled for replay. |
False
|
logger
|
Logger | None
|
Optional logger (creates default if not provided). |
None
|
Source code in src/infrastructure/sse/redis_subscriber.py
subscribe
async
¶
Subscribe to user's SSE event stream.
Returns an async generator that yields SSE events as they arrive. Subscribes to both user-specific and broadcast channels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
User ID to subscribe to. |
required |
categories
|
list[str] | None
|
Optional list of categories to filter. |
None
|
Yields:
| Name | Type | Description |
|---|---|---|
SSEEvent |
AsyncIterator[SSEEvent]
|
Events matching the subscription criteria. |
Source code in src/infrastructure/sse/redis_subscriber.py
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 | |
get_missed_events
async
¶
get_missed_events(
user_id: UUID,
last_event_id: UUID,
categories: list[str] | None = None,
) -> list[SSEEvent]
Get events missed since last_event_id.
When a client reconnects with Last-Event-ID, retrieves events that were published while disconnected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
UUID
|
User ID to get events for. |
required |
last_event_id
|
UUID
|
Last event ID received by client. |
required |
categories
|
list[str] | None
|
Optional category filter. |
None
|
Returns:
| Type | Description |
|---|---|
list[SSEEvent]
|
List of SSEEvent objects published after last_event_id. |
list[SSEEvent]
|
Empty list if retention disabled or no events found. |
Source code in src/infrastructure/sse/redis_subscriber.py
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 | |
filter_by_categories
¶
Check if event matches category filter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
SSEEvent
|
Event to check. |
required |
categories
|
list[str] | None
|
List of category strings to match. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if event matches filter, False otherwise. |
Source code in src/infrastructure/sse/redis_subscriber.py
validate_categories
staticmethod
¶
Validate and convert category strings to enum values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
categories
|
list[str] | None
|
List of category strings from query params. |
required |
Returns:
| Type | Description |
|---|---|
list[SSEEventCategory]
|
List of valid SSEEventCategory enum values. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any category string is invalid. |