Activation Contract
Load when adding real-time updates, emitting new SSE events, or extending useServerEvents.
Hard Rules
- Frontend: one SSE connection only — via
useServerEvents(hooks/useServerEvents.ts). Never open additionalEventSourceinstances. useServerEventsreacts to events by invalidating TanStack Query caches — NOT by writing to Zustand stores.- No reconnection logic exists — if needed, add it inside
useServerEvents, nowhere else. - Backend: emit SSE events via
infrastructure/messaging/app-event-bus.tsonly. Never emit from controllers directly. useServerEventsmust stay mounted inside the authenticated subtree under<TokenGate>(currentlyAuthenticatedAppin App.tsx). The/api/eventsSSE endpoint is gated byrequire-token; mounting SSE before unlock produces 401s. Never hoistuseServerEventsabove<TokenGate>.
Event Contracts
| Event name | Frontend reaction |
| -------------------------- | ------------------------------------------------------------------- |
| playlists-updated | invalidate playlists, downloadStatus, all tracks[*] |
| download-history-updated | invalidate downloadHistory |
| library-updated | invalidate libraryStats, libraryArtists, all libraryArtist[*] |
| feed-updated | invalidate releases, followedArtists |
Adding a New SSE Event
- Backend: emit via
app-event-bus.tswith a new event name string - Frontend: add a listener in
useServerEvents.ts, invalidate the relevant query keys fromqueryKeys.ts - Shared: if the event payload needs a typed shape, add it to
packages/shared/src/index.ts
References
- Frontend hook:
apps/frontend/src/hooks/useServerEvents.ts - Query keys:
apps/frontend/src/hooks/queryKeys.ts - Backend bus:
apps/backend/src/infrastructure/messaging/app-event-bus.ts - Backend controller:
apps/backend/src/presentation/controllers/events.controller.ts - Backend route:
apps/backend/src/presentation/routes/events.routes.ts