Live updates and notifications
Trace the complete delivery path before changing one end of it. A typical path is mutation → event creation → broker or transport → authenticated subscription → client state update or user alert.
Map the existing system
Identify:
- Where event names and payload schemas are defined.
- Which operation emits each event and whether it occurs before or after persistence commits.
- How recipients, tenants, rooms, topics, or channels are selected.
- Which transport is used, such as WebSocket, Socket.IO, SSE, push, or a message broker.
- How clients connect, authenticate, subscribe, reconnect, and disconnect.
- How consumers deduplicate events and update or invalidate state.
- Whether offline, mobile, email, or SMS fallback exists.
Reuse the project's established transport and event registry. Do not introduce a second connection singleton or parallel event taxonomy without a clear migration plan.
Define a stable event contract
An event should have enough information for routing and safe processing, commonly:
- A stable event name or type.
- A unique event identifier for deduplication.
- Resource or aggregate identity.
- Tenant or scope identity when applicable.
- A version when payload evolution requires it.
- The minimal payload consumers need.
Avoid placing secrets or unnecessary personal data in events. Prefer identifiers plus an authorized refetch when broadcasting full records would create privacy or staleness risks.
Keep producer and consumer constants or generated schemas synchronized through one authoritative source when the project supports it.
Emit safely
- Emit only after the underlying state change is confirmed.
- If delivery must survive process failure or be atomic with the write, use a transaction-aware outbox or the project's equivalent.
- Define whether delivery is at-most-once, at-least-once, or best effort; make consumers idempotent when duplicates are possible.
- Separate data-change signals from human-facing alerts when their payloads, urgency, or fallback behavior differ.
- Bound retries and surface permanent delivery failures to the system that owns them.
Authorize subscriptions and delivery
Authenticate connections using the project's supported mechanism. Authorize every room, topic, resource, or tenant subscription; possession of an identifier alone must not grant access.
Re-check authorization when account membership or permissions can change during a long-lived connection. Remove subscriptions and listeners during disconnect or scope changes.
Handle client lifecycle
- Keep one connection owner per intended application scope.
- Register and remove handlers symmetrically.
- Reconnect with bounded backoff and restore authorized subscriptions.
- Deduplicate by event ID when retries, multiple transports, or reconnect replay can duplicate delivery.
- Ignore events outside the active tenant or resource scope.
- Update a local cache directly only when the payload is complete and ordered; otherwise invalidate or refetch the affected data.
- Show offline or degraded state when users would otherwise assume updates are live.
Verify end to end
Test representative cases:
- Authorized delivery to the intended recipient.
- No delivery to unauthorized users or unrelated tenants.
- Emission only after a successful mutation.
- Duplicate and out-of-order event handling where applicable.
- Disconnect, reconnect, resubscription, and listener cleanup.
- Offline or fallback delivery behavior.
- Producer/consumer schema compatibility.
Use integration tests for room/topic routing and lifecycle behavior when unit tests cannot represent the transport accurately.
1---2name: live-notifications3description: Design, add, or debug real-time data updates and user alerts across backend delivery, shared event contracts, authorization, client subscriptions, reconnection, deduplication, and fallback channels. Use for WebSocket, SSE, broker-backed, push, or similar notification flows.4---56# Live updates and notifications78Trace the complete delivery path before changing one end of it. A typical path is mutation → event creation → broker or transport → authenticated subscription → client state update or user alert.910## Map the existing system1112Identify:13141. Where event names and payload schemas are defined.152. Which operation emits each event and whether it occurs before or after persistence commits.163. How recipients, tenants, rooms, topics, or channels are selected.174. Which transport is used, such as WebSocket, Socket.IO, SSE, push, or a message broker.185. How clients connect, authenticate, subscribe, reconnect, and disconnect.196. How consumers deduplicate events and update or invalidate state.207. Whether offline, mobile, email, or SMS fallback exists.2122Reuse the project's established transport and event registry. Do not introduce a second connection singleton or parallel event taxonomy without a clear migration plan.2324## Define a stable event contract2526An event should have enough information for routing and safe processing, commonly:2728- A stable event name or type.29- A unique event identifier for deduplication.30- Resource or aggregate identity.31- Tenant or scope identity when applicable.32- A version when payload evolution requires it.33- The minimal payload consumers need.3435Avoid placing secrets or unnecessary personal data in events. Prefer identifiers plus an authorized refetch when broadcasting full records would create privacy or staleness risks.3637Keep producer and consumer constants or generated schemas synchronized through one authoritative source when the project supports it.3839## Emit safely4041- Emit only after the underlying state change is confirmed.42- If delivery must survive process failure or be atomic with the write, use a transaction-aware outbox or the project's equivalent.43- Define whether delivery is at-most-once, at-least-once, or best effort; make consumers idempotent when duplicates are possible.44- Separate data-change signals from human-facing alerts when their payloads, urgency, or fallback behavior differ.45- Bound retries and surface permanent delivery failures to the system that owns them.4647## Authorize subscriptions and delivery4849Authenticate connections using the project's supported mechanism. Authorize every room, topic, resource, or tenant subscription; possession of an identifier alone must not grant access.5051Re-check authorization when account membership or permissions can change during a long-lived connection. Remove subscriptions and listeners during disconnect or scope changes.5253## Handle client lifecycle5455- Keep one connection owner per intended application scope.56- Register and remove handlers symmetrically.57- Reconnect with bounded backoff and restore authorized subscriptions.58- Deduplicate by event ID when retries, multiple transports, or reconnect replay can duplicate delivery.59- Ignore events outside the active tenant or resource scope.60- Update a local cache directly only when the payload is complete and ordered; otherwise invalidate or refetch the affected data.61- Show offline or degraded state when users would otherwise assume updates are live.6263## Verify end to end6465Test representative cases:66671. Authorized delivery to the intended recipient.682. No delivery to unauthorized users or unrelated tenants.693. Emission only after a successful mutation.704. Duplicate and out-of-order event handling where applicable.715. Disconnect, reconnect, resubscription, and listener cleanup.726. Offline or fallback delivery behavior.737. Producer/consumer schema compatibility.7475Use integration tests for room/topic routing and lifecycle behavior when unit tests cannot represent the transport accurately.