test sse fixes

This commit is contained in:
yohlo
2025-09-29 21:28:22 -05:00
parent 5094933302
commit baf75eddba
2 changed files with 20 additions and 7 deletions

View File

@@ -37,13 +37,22 @@ export const Route = createFileRoute("/api/events/$")({
clearInterval(pingInterval); clearInterval(pingInterval);
return; return;
} }
const pingMessage = `data: ${JSON.stringify({ type: "ping" })}\n\n`; const pingMessage = `data: ${JSON.stringify({ type: "ping", timestamp: Date.now() })}\n\n`;
controller.enqueue(new TextEncoder().encode(pingMessage)); controller.enqueue(new TextEncoder().encode(pingMessage));
} catch (e) { } catch (e) {
logger.error("ServerEvents | Ping interval error", e); logger.error("ServerEvents | Ping interval error", e);
clearInterval(pingInterval); clearInterval(pingInterval);
} }
}, 30000); }, 15000);
setTimeout(() => {
try {
const heartbeatMessage = `data: ${JSON.stringify({ type: "heartbeat", timestamp: Date.now() })}\n\n`;
controller.enqueue(new TextEncoder().encode(heartbeatMessage));
} catch (e) {
logger.error("ServerEvents | Heartbeat error", e);
}
}, 1000);
const cleanup = () => { const cleanup = () => {
serverEvents.off("test", handleEvent); serverEvents.off("test", handleEvent);
@@ -63,6 +72,9 @@ export const Route = createFileRoute("/api/events/$")({
"Access-Control-Allow-Origin": "*", "Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Cache-Control", "Access-Control-Allow-Headers": "Cache-Control",
"X-Accel-Buffering": "no", "X-Accel-Buffering": "no",
"X-Proxy-Buffering": "no",
"Proxy-Buffering": "off",
"Transfer-Encoding": "chunked",
}, },
}); });
}, },

View File

@@ -19,6 +19,7 @@ const eventHandlers: Record<string, EventHandler> = {
logger.info("New Connection"); logger.info("New Connection");
}, },
"ping": () => {}, "ping": () => {},
"heartbeat": () => {},
"match": (event, queryClient) => { "match": (event, queryClient) => {
queryClient.invalidateQueries(tournamentQueries.details(event.tournamentId)) queryClient.invalidateQueries(tournamentQueries.details(event.tournamentId))
queryClient.invalidateQueries(tournamentQueries.current()) queryClient.invalidateQueries(tournamentQueries.current())
@@ -73,15 +74,15 @@ export function useServerEvents() {
logger.error("SSE connection error", error); logger.error("SSE connection error", error);
eventSource.close(); eventSource.close();
if (shouldConnectRef.current && retryCountRef.current < 5) { if (shouldConnectRef.current && retryCountRef.current < 10) {
retryCountRef.current += 1; retryCountRef.current += 1;
const delay = Math.min( const delay = Math.min(
1000 * Math.pow(2, retryCountRef.current - 1), 1000 * Math.pow(1.5, retryCountRef.current - 1),
30000 15000
); );
logger.info( logger.info(
`SSE reconnection attempt ${retryCountRef.current}/5 in ${delay}ms` `SSE reconnection attempt ${retryCountRef.current}/10 in ${delay}ms`
); );
timeoutRef.current = setTimeout(() => { timeoutRef.current = setTimeout(() => {
@@ -89,7 +90,7 @@ export function useServerEvents() {
connectEventSource(); connectEventSource();
} }
}, delay); }, delay);
} else if (retryCountRef.current >= 5) { } else if (retryCountRef.current >= 10) {
logger.error("SSE max reconnection attempts reached"); logger.error("SSE max reconnection attempts reached");
} }
}; };