From 7963aad5ee8de3397162448ef85b8fd7edca9f97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 24 Jul 2026 01:38:18 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5=20feat(exams):=20poll-only=20live?= =?UTF-8?q?=20monitor,=20drop=20the=20SSE=20path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The monitor opened an EventSource AND ran a 2s poll at once; both ticked at the same cadence, and the SSE half was killed on its first error and never retried — pure redundancy plus double backend load. Keep the 2s poll against GET /api/exams/:id/live as the sole feed. Removes the EventSource wiring + applyStreamEvent, deletes getExamLiveStreamUrl and its barrel export, and drops the two stream-URL tests. --- nginx.conf | 36 --------------- nginx.conf.test.ts | 33 -------------- src/api/__tests__/exams/exam-live.test.ts | 11 ----- src/api/exams/getExamLiveStreamUrl.ts | 3 -- src/api/exams/index.ts | 1 - src/pages/live-monitor-page.tsx | 55 ++++------------------- 6 files changed, 8 insertions(+), 131 deletions(-) delete mode 100644 nginx.conf delete mode 100644 nginx.conf.test.ts delete mode 100644 src/api/exams/getExamLiveStreamUrl.ts diff --git a/nginx.conf b/nginx.conf deleted file mode 100644 index 13864e5..0000000 --- a/nginx.conf +++ /dev/null @@ -1,36 +0,0 @@ -server { - listen 5173; - root /usr/share/nginx/html; - index index.html; - - # Proxy the whole /api/* namespace, not a hand-maintained allowlist: an - # enumerated list silently 404s (falls through to the SPA -> HTML -> the - # client's JSON.parse fails) every time the backend or frontend gains a - # resource the list forgot. homework, messages, and questions were all - # already broken this way. - location /api/ { - rewrite ^/api(/.*)$ $1 break; - proxy_pass http://hezarfen-backend:8080; - proxy_http_version 1.1; - proxy_set_header Host $http_host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection "upgrade"; - } - - location /assets/ { - try_files $uri =404; - add_header Cache-Control "public, max-age=31536000, immutable" always; - } - - location = /index.html { - add_header Cache-Control "no-store" always; - } - - location / { - try_files $uri /index.html; - add_header Cache-Control "no-store" always; - } -} diff --git a/nginx.conf.test.ts b/nginx.conf.test.ts deleted file mode 100644 index ac6c078..0000000 --- a/nginx.conf.test.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { expect, test } from "bun:test"; -import { readFileSync } from "node:fs"; - -// Mirror of backend route mounts (hezarfen_backend src/lib.rs .nest calls + /health, /time). -// Backend adds a prefix → add it here AND in nginx.conf's proxy location. -const BACKEND_PREFIXES = [ - "auth", - "users", - "notes", - "events", - "courses", - "sessions", - "exams", - "marks", - "work", - "pomodoro", - "attendance", - "settings", - "subjects", - "terms", - "health", - "time", -]; - -test("nginx proxy whitelist covers every backend route prefix", () => { - const conf = readFileSync(new URL("./nginx.conf", import.meta.url), "utf8"); - const match = conf.match(/\^\/api\/\(([^)]+)\)/); - expect(match).not.toBeNull(); - const listed = match![1].split("|"); - for (const prefix of BACKEND_PREFIXES) { - expect(listed).toContain(prefix); - } -}); diff --git a/src/api/__tests__/exams/exam-live.test.ts b/src/api/__tests__/exams/exam-live.test.ts index 123115d..6e4b69f 100644 --- a/src/api/__tests__/exams/exam-live.test.ts +++ b/src/api/__tests__/exams/exam-live.test.ts @@ -1,6 +1,5 @@ import { afterEach, describe, expect, it, vi } from "vitest"; import { getExamLive } from "../../exams"; -import { getExamLiveStreamUrl } from "../../exams"; import { lastFetchCall, mockFetchSuccess } from "../helpers/mock-fetch"; describe("exams API - live", () => { @@ -19,14 +18,4 @@ describe("exams API - live", () => { expect(url).toBe("/api/exams/ex1/live"); expect(init?.method).toBe("GET"); }); - - it("getExamLiveStreamUrl returns correct URL", () => { - const url = getExamLiveStreamUrl("ex1"); - expect(url).toBe("/api/exams/ex1/live/stream"); - }); - - it("getExamLiveStreamUrl encodes examId correctly", () => { - const url = getExamLiveStreamUrl("ex1/abc?xyz"); - expect(url).toBe("/api/exams/ex1%2Fabc%3Fxyz/live/stream"); - }); }); diff --git a/src/api/exams/getExamLiveStreamUrl.ts b/src/api/exams/getExamLiveStreamUrl.ts deleted file mode 100644 index 1cd6321..0000000 --- a/src/api/exams/getExamLiveStreamUrl.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function getExamLiveStreamUrl(examId: string): string { - return `/api/exams/${encodeURIComponent(examId)}/live/stream`; -} diff --git a/src/api/exams/index.ts b/src/api/exams/index.ts index 9bb9ade..e9f193c 100644 --- a/src/api/exams/index.ts +++ b/src/api/exams/index.ts @@ -18,7 +18,6 @@ export * from "./deleteExamResultByUserId"; export * from "./getExamStatistics"; export * from "./getStudentAnswers"; export * from "./getExamLive"; -export * from "./getExamLiveStreamUrl"; export * from "./getExamAttempt"; export * from "./postExamAttempt"; export * from "./postExamAttemptFinish"; diff --git a/src/pages/live-monitor-page.tsx b/src/pages/live-monitor-page.tsx index 1e827e6..e18eaf0 100644 --- a/src/pages/live-monitor-page.tsx +++ b/src/pages/live-monitor-page.tsx @@ -2,7 +2,6 @@ import { Show, Suspense, createEffect, createMemo, createResource, createSignal, import { Link, useLocation, useParams } from "@tanstack/solid-router"; import type { ColumnDef } from "@tanstack/solid-table"; import { getExamLive } from "@/api/exams"; -import { getExamLiveStreamUrl } from "@/api/exams"; import { getExamById } from "@/api/exams"; import { formatApiError } from "@/api/client"; import type { LiveMonitor, LiveRosterEntry } from "@/api/client"; @@ -142,15 +141,6 @@ function LiveMonitorContent() { } }; - const applyStreamEvent = (event: MessageEvent) => { - try { - setSnapshot(JSON.parse(event.data) as LiveMonitor); - setError(""); - } catch (err) { - console.error("[live-monitor] stream parse error:", err); - } - }; - createEffect(() => { const eid = id(); const e = exam(); @@ -158,45 +148,16 @@ function LiveMonitorContent() { void fetchSnapshot(); if (e.ends_at != null && e.ends_at < untrack(now)) return; - let interval: ReturnType | null = null; - const startPolling = () => { - if (interval) return; - interval = setInterval(() => { - if (e.ends_at != null && e.ends_at < untrack(now)) { - if (interval) clearInterval(interval); - interval = null; - void fetchSnapshot(); - return; - } - void fetchSnapshot(); - }, 2000); - }; - - // ponytail: keep polling even with SSE; some live proxies accept EventSource - // but never flush events. 2s GET is cheaper than stale teacher screen. - startPolling(); - - if (!("EventSource" in window)) { - onCleanup(() => { - if (interval) clearInterval(interval); - }); - return; - } - - const source = new EventSource(getExamLiveStreamUrl(eid), { withCredentials: true }); - source.addEventListener("snapshot", applyStreamEvent); - source.onmessage = applyStreamEvent; - source.onerror = () => { - source.close(); - startPolling(); - }; - - onCleanup(() => { - source.close(); - if (interval) { + const interval = setInterval(() => { + if (e.ends_at != null && e.ends_at < untrack(now)) { clearInterval(interval); + void fetchSnapshot(); + return; } - }); + void fetchSnapshot(); + }, 2000); + + onCleanup(() => clearInterval(interval)); }); const liveRows = createMemo(() => {