Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : UQO Éval — Le Grand Défi Immobilier8 * File : apps/api/src/live.ts9 * Module : api10 * Repository : https://git.spboucher.ai/uqo-eval-jeux11 * Project Website : https://jeux.uqo-eval.app12 * Created : 2026-11-2513 * Last Modified : 2026-11-2514 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development : dev@spboucher.ai24 * Support : support@spboucher.ai25 * Security/Admin : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243/**44 * Diffusion en direct (Server-Sent Events) : le classement se met à jour sans45 * rechargement. Les messages ne portent aucune donnée : ils invitent le46 * navigateur à relire le classement.47 */48import type { ServerResponse } from "node:http";4950const channels = new Map<string, Set<ServerResponse>>();51const pending = new Map<string, NodeJS.Timeout>();5253export function subscribe(channel: string, res: ServerResponse): void {54 const set = channels.get(channel) ?? new Set();55 set.add(res);56 channels.set(channel, set);57 res.on("close", () => {58 set.delete(res);59 if (set.size === 0) channels.delete(channel);60 });61}6263/** Publie un événement, regroupé sur 400 ms pour ne pas inonder les clients. */64export function publish(channel: string, event: "progress" | "completed" | "admin"): void {65 const key = `${channel}|${event}`;66 if (pending.has(key)) return;67 pending.set(68 key,69 setTimeout(() => {70 pending.delete(key);71 const set = channels.get(channel);72 if (!set) return;73 const payload = `event: ${event}\ndata: ${Date.now()}\n\n`;74 for (const res of set) res.write(payload);75 }, 400),76 );77}7879export function listenerCount(): number {80 let n = 0;81 for (const set of channels.values()) n += set.size;82 return n;83}8485const heartbeat = setInterval(() => {86 for (const set of channels.values()) for (const res of set) res.write(": ping\n\n");87}, 25_000);88heartbeat.unref();89