domain.events.sse_event¶
src.domain.events.sse_event
¶
Server-Sent Events (SSE) event types and data structures.
This module defines the SSE event format for real-time client notifications. SSE events are the wire format sent to clients - they're distinct from domain events, which represent internal business occurrences.
Architecture
- SSEEvent: Immutable dataclass representing an SSE message
- SSEEventType: Enum of all valid event types (SSOT)
- SSEEventCategory: Categories for client-side filtering
- to_sse_format(): Serialization to SSE wire format (text/event-stream)
Wire Format (SSE spec):
id:
Reference
- docs/architecture/sse-architecture.md
- https://html.spec.whatwg.org/multipage/server-sent-events.html
Classes¶
SSEEventCategory
¶
Bases: StrEnum
Categories for SSE event filtering.
Clients can subscribe to specific categories via query params
GET /api/v1/events?categories=data_sync,provider
Categories group related event types for efficient filtering.
Source code in src/domain/events/sse_event.py
Attributes¶
DATA_SYNC
class-attribute
instance-attribute
¶
Account, transaction, and holdings sync events.
PROVIDER
class-attribute
instance-attribute
¶
Provider connection health and token events.
PORTFOLIO
class-attribute
instance-attribute
¶
Balance and holdings update events.
SECURITY
class-attribute
instance-attribute
¶
Session and security notification events.
SSEEventType
¶
Bases: StrEnum
All SSE event types (Single Source of Truth).
Event naming convention
{category}.{resource}.{action}
Examples:
- sync.accounts.started
- provider.token.expiring
- ai.response.chunk
Note
These are SSE event types sent to clients, NOT domain event names. Domain events use PascalCase (AccountSyncSucceeded). SSE events use dot-notation (sync.accounts.completed).
Source code in src/domain/events/sse_event.py
SSEEvent
dataclass
¶
Server-Sent Event data structure.
Represents a single SSE message to be sent to connected clients. Immutable after creation (frozen dataclass).
Attributes:
| Name | Type | Description |
|---|---|---|
event_id |
UUID
|
Unique identifier for this event (UUID v7 for ordering). |
event_type |
SSEEventType
|
Type of event (from SSEEventType enum). |
user_id |
UUID
|
Target user for this event (for channel routing). |
data |
dict[str, Any]
|
Event payload (will be JSON serialized). |
occurred_at |
datetime
|
When the event occurred (UTC). |
Example
event = SSEEvent( ... event_type=SSEEventType.SYNC_ACCOUNTS_COMPLETED, ... user_id=user_id, ... data={"connection_id": str(conn_id), "account_count": 3}, ... ) print(event.to_sse_format()) id: 01234567-89ab-cdef-0123-456789abcdef event: sync.accounts.completed data: {"connection_id": "...", "account_count": 3}
Source code in src/domain/events/sse_event.py
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 | |
Attributes¶
event_type
instance-attribute
¶
Type of SSE event (determines client handling).
event_id
class-attribute
instance-attribute
¶
Unique event identifier (UUID v7 for temporal ordering).
occurred_at
class-attribute
instance-attribute
¶
Timestamp when event occurred (UTC).
category
property
¶
Get the category for this event type.
Returns:
| Type | Description |
|---|---|
SSEEventCategory
|
The category this event belongs to. |
Functions¶
to_sse_format
¶
Serialize to SSE wire format.
Returns:
| Type | Description |
|---|---|
str
|
SSE-formatted string ready for streaming response. |
Example output
id: 01234567-89ab-cdef-0123-456789abcdef event: sync.accounts.completed data: {"connection_id": "abc", "account_count": 3}
Note
- Each field ends with newline
- Message ends with double newline (SSE spec)
- Data is JSON serialized
- Comments (lines starting with :) are ignored by clients
Source code in src/domain/events/sse_event.py
to_dict
¶
Convert to dictionary for Redis storage/transmission.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary representation of the event. |
Source code in src/domain/events/sse_event.py
from_dict
classmethod
¶
Create SSEEvent from dictionary (Redis deserialization).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Dictionary with event data. |
required |
Returns:
| Type | Description |
|---|---|
SSEEvent
|
SSEEvent instance. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If required fields are missing. |
ValueError
|
If field values are invalid. |
Source code in src/domain/events/sse_event.py
Functions¶
get_category_for_event_type
¶
Get the category for an SSE event type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_type
|
SSEEventType
|
The SSE event type. |
required |
Returns:
| Type | Description |
|---|---|
SSEEventCategory
|
The category the event type belongs to. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If event type is not mapped (indicates registry bug). |