const clients = new Set();

function subscribe(res) { clients.add(res); }
function unsubscribe(res) { clients.delete(res); }

function broadcast(event, data) {
    const msg = `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`;
    for (const res of clients) {
        try { res.write(msg); } catch (_) { clients.delete(res); }
    }
}

module.exports = { subscribe, unsubscribe, broadcast };
