Path: blob/master/webhooks/webhook_preview/lib/sse-store.ts
1085 views
import { WebhookData } from "@/types/webhook";12type Client = {3id: string;4res: Response;5};67const clients: Map<string, Client[]> = new Map();89export function addClient(uuid: string, client: Client) {10const current = clients.get(uuid) || [];11clients.set(uuid, [...current, client]);12}1314export function removeClient(uuid: string, clientId: string) {15const current = clients.get(uuid) || [];16clients.set(17uuid,18current.filter((c) => c.id !== clientId),19);20}2122export function sendEvent(uuid: string, data: WebhookData) {23const current = clients.get(uuid) || [];24current.forEach((client) => {25try {26client.res.write?.(`data: ${JSON.stringify(data)}\n\n`);27} catch (err) {28console.error("Erro ao enviar SSE:", err);29}30});31}323334