Workflow: Integrate WebSockets (Realtime)
Objective
Add a reliable realtime feature: an authenticated WebSocket transport with heartbeats, reconnection, backpressure, and typed events exposed to the UI as an AsyncStream.
Inputs
- WebSocket URL, auth scheme, message/event schema, ordering/delivery guarantees.
- Backpressure expectations (message rate, criticality).
Outputs
- Actor-based transport (state machine), typed event mappers, repository/stream, UI state, tests.
Step-by-Step Process
- Design (iOS Architect + WebSocket Expert) — define the connection state machine, event types, and buffering/overflow policy (see
architecture/websocket_architecture.md). - Build the transport as an
actor: connect, authenticate, heartbeat, receive loop, capped backoff reconnection (seeskills/networking/ios/websocket.md). - Decode events into typed domain events; de-duplicate by id.
- Apply backpressure — bounded buffer with drop/coalesce policy.
- Auth (Security Expert) — refresh token on each (re)connect; no token leakage.
- Expose state (
connecting/connected/reconnecting/disconnected) to the UI. - Performance (Performance Expert) — verify no battery/ memory issues under load.
- Test connection lifecycle, reconnection, and decoding; review against
checklists/websocket_review.md.
Validation Steps
- Survives network drop: detects via heartbeat and reconnects with backoff.
- Re-authenticates on reconnect; no duplicate events shown.
- Buffer bounded under load; UI reflects connection state.
Failure Scenarios
- Silent dead connection → heartbeat + read timeout must catch it.
- Reconnect storm → ensure capped backoff + jitter.
- Token expiry across reconnect → refresh before reconnecting.
- Consumer flooded → apply the defined overflow policy.
AI Agent Instructions
- Model the connection as an explicit state machine; make the transport an actor.
- Always implement heartbeat + backoff reconnection; re-auth on reconnect.
- Expose
AsyncStream<DomainEvent>; de-dup by id; bound buffers. - Clean up tasks/sockets on teardown.
Acceptance Criteria
- [ ] Actor transport with state machine, heartbeat, backoff reconnection.
- [ ] Re-auth on reconnect; events de-duplicated.
- [ ] Bounded buffering; UI shows connection state.
- [ ] Lifecycle + decoding tested;
checklists/websocket_review.mdpasses.