// In-process pub/sub for realtime orders over SSE. // One Next.js instance per Coolify deploy makes this sufficient; swap for Redis // pub/sub if the app is ever scaled horizontally. type Listener = (event: { type: string; data: unknown }) => void; const channels = new Map>(); function globalBus(): Map> { const g = globalThis as unknown as { __ordersBus?: Map> }; if (!g.__ordersBus) g.__ordersBus = channels; return g.__ordersBus; } export function subscribe(slug: string, listener: Listener): () => void { const bus = globalBus(); let set = bus.get(slug); if (!set) { set = new Set(); bus.set(slug, set); } set.add(listener); return () => { set?.delete(listener); if (set && set.size === 0) bus.delete(slug); }; } export function publish(slug: string, event: { type: string; data: unknown }): void { const bus = globalBus(); bus.get(slug)?.forEach((l) => l(event)); }