import { Express, Response } from 'express';
let clients: Response[] = [];
export function setupSSE(app: Express) {
app.get('/sse', (req, res) => {
res.set({
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
res.flushHeaders();
res.write(': connected\n\n');
clients.push(res);
req.on('close', () => {
clients = clients.filter(c => c !== res);
});
});
}
export function broadcastSSE(data: any) {
for (const client of clients) {
client.write(`data: ${JSON.stringify(data)}\n\n`);
}
}