Chat Event Bus

clawsuite · integration, sse, event-bus, deduplication, pubsub, realtime

Singleton SSE event bus that deduplicates gateway events and broadcasts processed events to N clients. Solves the N-listener duplication problem by funneling all gateway events through one listener then broadcasting.

/**
 * Singleton event bus for chat SSE events.
 *
 * Architecture: ONE gateway listener → processes/deduplicates → broadcasts to N SSE clients.
 * Previously each SSE connection registered its own onGatewayEvent listener,
 * causing 1 gateway event × N listeners = N duplicate emissions.
 *
 * Now: 1 gateway event → 1 listener → 1 processed event → broadcast to N clients.
 */
import {
  onGatewayEvent,
  gatewayConnectCheck,
  hasActiveSendRun,
} from './gateway'
import type { GatewayFrame } from './gateway'

export interface ChatSSEEvent {
  event: string
  data: Record<string, unknown>
}

type ChatSSESubscriber = (event: ChatSSEEvent) => void

// ─── Singleton state (survives Vite HMR via globalThis) ─────────────────

const BUS_KEY = '__clawsuite_chat_event_bus__' as const

interface Bu

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search 40K more like it.

Get a free API key