TanStack Query SSE in React: authenticated live updates
Polling a dashboard every few seconds is easy to ship. It also means sending requests when nothing has changed and making users wait for the next interval before they see an update.
If the backend already exposes Server-Sent Events (SSE), the browser can receive changes as they happen. The trick is to keep the stream out of your state-management layer:
TanStack Query owns the cached server state.
SSE delivers one-way updates from the server.
Your event handler updates or invalidates the Query cache.
This example uses React, TanStack Query, and tanstack-fetch , an independent community project (not an official TanStack package).
Install and configure the client
npm install tanstack-fetch @tanstack/react-query
Enter fullscreen mode
Exit fullscreen mode
The SSE entry includes the normal HTTP methods and api.sse() :
import { createFetch } from 'tanstack-fetch/sse'
export const api = createFetch ( { baseUrl : import . meta . env .…