This is related to #580 and a PR fixing this should target the tanstack-db branch.
Problem
In src/lib/hooks/useRealtimeUpdates.ts:666-714, the createConnection function makes a fetch() request to /api/v1/events to check for a 503 status (Redis down), then immediately cancels the response body and opens a second connection via EventSource to the same URL.
const createConnection = async () => {
// First request: probe for 503
const response = await fetch("/api/v1/events", {
method: "GET",
credentials: "include",
headers: { Accept: "text/event-stream" },
});
if (response.status === 503) { /* switch to polling */ return; }
if (!response.ok) { throw new Error(...); }
// Cancel the first SSE stream we just opened
response.body?.cancel();
// Second request: actual EventSource connection
const eventSource = new EventSource("/api/v1/events", { withCredentials: true });
// ...
};
This means:
- The server opens an SSE connection, subscribes to Redis channels, sends the connection handshake
- The client immediately cancels that connection
- A second SSE connection is opened, repeating all the server-side setup
Impact
- Two HTTP requests per connection attempt
- Server does unnecessary work setting up and tearing down the probe connection
- Brief window where the server has two open connections for the same user
Possible solutions
-
Use EventSource directly and handle errors in onerror. EventSource fires onerror for non-200 responses. The challenge is distinguishing "Redis is down (503)" from other errors, since EventSource doesn't expose HTTP status codes.
-
Use a lightweight health check endpoint (e.g., HEAD /api/v1/events/health or GET /api/v1/events?probe=true) that returns 200/503 without opening an SSE stream, then open EventSource only on success.
-
Start with EventSource optimistically and switch to polling if it fails to connect after a short timeout or repeated errors. This is simpler and handles the common case (Redis is up) with a single request.
Option 2 or 3 would be cleanest.
This is related to #580 and a PR fixing this should target the
tanstack-dbbranch.Problem
In
src/lib/hooks/useRealtimeUpdates.ts:666-714, thecreateConnectionfunction makes afetch()request to/api/v1/eventsto check for a 503 status (Redis down), then immediately cancels the response body and opens a second connection viaEventSourceto the same URL.This means:
Impact
Possible solutions
Use EventSource directly and handle errors in
onerror. EventSource firesonerrorfor non-200 responses. The challenge is distinguishing "Redis is down (503)" from other errors, since EventSource doesn't expose HTTP status codes.Use a lightweight health check endpoint (e.g.,
HEAD /api/v1/events/healthorGET /api/v1/events?probe=true) that returns 200/503 without opening an SSE stream, then open EventSource only on success.Start with EventSource optimistically and switch to polling if it fails to connect after a short timeout or repeated errors. This is simpler and handles the common case (Redis is up) with a single request.
Option 2 or 3 would be cleanest.