forge
Explore
Log in

Don't you dare go Hollow

✨ stream live exam monitor updates

Browse files at this commit

5ed887e4c1234a354d2b3cb064f669c5cd341f32Burak BoduroğluParent: 41442f8
  • feat
    • ✨ stream live exam monitor updates
Show full message
feat: ✨ stream live exam monitor updates

- add a live monitor SSE URL helper for /exams/{id}/live/stream

- consume snapshot events with EventSource on the live monitor page

- keep the existing one-shot snapshot and polling fallback when streaming fails

- stop continuous updates for exams whose window has already ended
src/api/getExamLiveStreamUrl.tsadded+3 -0
@@ -0,0 +1,3 @@
1+export function getExamLiveStreamUrl(examId: string): string {
2+ return `/api/exams/${encodeURIComponent(examId)}/live/stream`;
3+}
src/pages/live-monitor-page.tsxmodified+48 -9
@@ -1,6 +1,7 @@
1-import { For, Show, Suspense, createEffect, createResource, createSignal, onCleanup } from "solid-js";1+import { For, Show, Suspense, createEffect, createResource, createSignal, onCleanup, untrack } from "solid-js";
2 import { Link, useParams } from "@tanstack/solid-router";2 import { Link, useParams } from "@tanstack/solid-router";
3 import { getExamLive } from "@/api/getExamLive";3 import { getExamLive } from "@/api/getExamLive";
4+import { getExamLiveStreamUrl } from "@/api/getExamLiveStreamUrl";
4 import { getExamById } from "@/api/getExamById";5 import { getExamById } from "@/api/getExamById";
5 import { formatApiError } from "@/api/client";6 import { formatApiError } from "@/api/client";
6 import type { LiveMonitor, LiveRosterEntry } from "@/api/types";7 import type { LiveMonitor, LiveRosterEntry } from "@/api/types";
@@ -107,27 +108,65 @@ function LiveMonitorContent() {
107 try {108 try {
108 const data = await getExamLive(id());109 const data = await getExamLive(id());
109 setSnapshot(data);110 setSnapshot(data);
111+ setError("");
110 } catch (err) {112 } catch (err) {
111 console.error("[live-monitor] fetch error:", err);113 console.error("[live-monitor] fetch error:", err);
112 setError(formatApiError(err, locale()));114 setError(formatApiError(err, locale()));
113 }115 }
114 };116 };
115 117
118+ const applyStreamEvent = (event: MessageEvent) => {
119+ try {
120+ setSnapshot(JSON.parse(event.data) as LiveMonitor);
121+ setError("");
122+ } catch (err) {
123+ console.error("[live-monitor] stream parse error:", err);
124+ }
125+ };
126+
116 createEffect(() => {127 createEffect(() => {
117 const eid = id();128 const eid = id();
118 const e = exam();129 const e = exam();
119 if (!eid || !e) return;130 if (!eid || !e) return;
120 void fetchSnapshot();131 void fetchSnapshot();
121- if (e.ends_at != null && e.ends_at < now()) return;132+ if (e.ends_at != null && e.ends_at < untrack(now)) return;
122- const interval = setInterval(() => {133+
123- if (e.ends_at != null && e.ends_at < now()) {134+ let interval: ReturnType<typeof setInterval> | null = null;
124- clearInterval(interval);135+ const startPolling = () => {
136+ if (interval) return;
137+ interval = setInterval(() => {
138+ if (e.ends_at != null && e.ends_at < untrack(now)) {
139+ if (interval) clearInterval(interval);
140+ interval = null;
141+ void fetchSnapshot();
142+ return;
143+ }
125 void fetchSnapshot();144 void fetchSnapshot();
126- return;145+ }, 2000);
146+ };
147+
148+ if (!("EventSource" in window)) {
149+ startPolling();
150+ onCleanup(() => {
151+ if (interval) clearInterval(interval);
152+ });
153+ return;
154+ }
155+
156+ const source = new EventSource(getExamLiveStreamUrl(eid), { withCredentials: true });
157+ source.addEventListener("snapshot", applyStreamEvent);
158+ source.onmessage = applyStreamEvent;
159+ source.onerror = () => {
160+ source.close();
161+ startPolling();
162+ };
163+
164+ onCleanup(() => {
165+ source.close();
166+ if (interval) {
167+ clearInterval(interval);
127 }168 }
128- void fetchSnapshot();169+ });
129- }, 2000);
130- onCleanup(() => clearInterval(interval));
131 });170 });
132 171
133 const toggleSort = (key: SortKey) => {172 const toggleSort = (key: SortKey) => {