2026-06-02 00:11:29 +07:00
|
|
|
/**
|
2026-06-09 11:56:03 +07:00
|
|
|
* Broadcast functions for WebSocket events.
|
2026-06-02 00:11:29 +07:00
|
|
|
*
|
2026-06-09 11:56:03 +07:00
|
|
|
* These are injected by ws/server.ts when the WebSocket server initializes.
|
2026-06-02 00:11:29 +07:00
|
|
|
* Other modules call them to push real-time events to connected frontend clients.
|
|
|
|
|
*
|
|
|
|
|
* Usage:
|
2026-06-09 22:04:05 +07:00
|
|
|
* import { broadcastEvent } from "../ws/broadcast.js";
|
|
|
|
|
* broadcastEvent("message_created", messageData);
|
2026-06-02 00:11:29 +07:00
|
|
|
*/
|
|
|
|
|
|
2026-06-09 19:46:08 +07:00
|
|
|
import { createChildLogger } from "@bete/shared/logger";
|
|
|
|
|
|
|
|
|
|
const logger = createChildLogger("broadcast");
|
|
|
|
|
|
2026-06-09 22:04:05 +07:00
|
|
|
type BroadcastFn = (type: string, data: unknown) => void;
|
2026-06-08 19:14:34 +07:00
|
|
|
type BroadcastBinaryFn = (data: Buffer) => void;
|
2026-06-02 00:11:29 +07:00
|
|
|
|
2026-06-09 22:04:05 +07:00
|
|
|
let _broadcast: BroadcastFn | null = null;
|
|
|
|
|
let _broadcastBinary: BroadcastBinaryFn | null = null;
|
2026-06-09 11:56:03 +07:00
|
|
|
|
2026-06-09 19:46:08 +07:00
|
|
|
let _enabled = true;
|
|
|
|
|
|
|
|
|
|
/** Enable or disable broadcast logging (disabled by default to reduce noise). */
|
|
|
|
|
export function setBroadcastLogging(enabled: boolean): void {
|
|
|
|
|
_enabled = enabled;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-09 11:56:03 +07:00
|
|
|
/**
|
|
|
|
|
* Inject broadcast functions from the WebSocket server initializer.
|
|
|
|
|
* Must be called once during server startup before any broadcast is used.
|
|
|
|
|
*/
|
2026-06-09 22:04:05 +07:00
|
|
|
export function setBroadcastFunctions(
|
|
|
|
|
bf: BroadcastFn,
|
|
|
|
|
bfBinary: BroadcastBinaryFn,
|
|
|
|
|
): void {
|
|
|
|
|
_broadcast = bf;
|
|
|
|
|
_broadcastBinary = bfBinary;
|
2026-06-09 19:46:08 +07:00
|
|
|
logger.info("Broadcast functions initialized");
|
2026-06-09 11:56:03 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Clear injected functions (used during cleanup). */
|
|
|
|
|
export function clearBroadcastFunctions(): void {
|
2026-06-09 22:04:05 +07:00
|
|
|
_broadcast = null;
|
|
|
|
|
_broadcastBinary = null;
|
2026-06-09 19:46:08 +07:00
|
|
|
logger.info("Broadcast functions cleared");
|
2026-06-09 11:56:03 +07:00
|
|
|
}
|
|
|
|
|
|
2026-06-09 22:04:05 +07:00
|
|
|
function shouldLog(type: string): boolean {
|
|
|
|
|
if (!_enabled) return false;
|
|
|
|
|
// Avoid logging high-volume events
|
|
|
|
|
if (type === "voice_pcm_data") return false;
|
|
|
|
|
return true;
|
2026-06-09 19:46:08 +07:00
|
|
|
}
|
2026-06-02 00:11:29 +07:00
|
|
|
|
2026-06-09 22:04:05 +07:00
|
|
|
/**
|
|
|
|
|
* Broadcast a JSON event to all connected WebSocket clients.
|
|
|
|
|
*/
|
|
|
|
|
export function broadcastEvent(type: string, data: unknown): void {
|
|
|
|
|
if (shouldLog(type)) {
|
|
|
|
|
logger.debug({ event: type }, "Broadcasting event");
|
|
|
|
|
}
|
|
|
|
|
_broadcast?.(type, data);
|
|
|
|
|
}
|
2026-06-02 00:11:29 +07:00
|
|
|
|
2026-06-09 22:04:05 +07:00
|
|
|
/**
|
|
|
|
|
* Broadcast binary data to all connected WebSocket clients.
|
|
|
|
|
*/
|
|
|
|
|
export function broadcastBinary(data: Buffer): void {
|
|
|
|
|
_broadcastBinary?.(data);
|
|
|
|
|
}
|