diff --git a/src/lib/realtime/bus.ts b/src/lib/realtime/bus.ts index 463c85bb..30c90fe9 100644 --- a/src/lib/realtime/bus.ts +++ b/src/lib/realtime/bus.ts @@ -5,7 +5,21 @@ export interface SSEEventClient { presenceChannel?: string; } -const clients = new Set(); +/** + * The connected-client set is anchored on `globalThis`, not module scope. Next.js + * dev bundles server packages per route (`devBundleServerPackages`), so a plain + * module-level Set forks into one copy per bundle: a broadcast issued from a + * server-action or registry bundle would never reach a client subscribed through + * the `/api/realtime` route bundle. Sharing it via `globalThis` (the same trick + * the blackjack/poker stores use, and how Payload caches its instance) unifies + * every bundle within the single dev process. In production there is one module + * instance anyway, so this is a no-op there. + */ +interface BusRegistry { + __ptfSseClients?: Set; +} +const globalBus = globalThis as typeof globalThis & BusRegistry; +const clients: Set = (globalBus.__ptfSseClients ??= new Set()); export function subscribe(client: SSEEventClient): () => void { clients.add(client);