---
title: "Vite · React · TanStack Query · HTTP polling"
url: "https://spinetab.com/docs/recipes/vite-react/query/polling/"
description: "HTTP polling with TanStack Query in Vite · React: dependencies, configuration, component and cleanup."
---

# Vite · React · TanStack Query · HTTP polling

Feed shared updates into your existing query cache.

**Available recipes**

- [Vite · React · TanStack Query · HTTP polling](https://spinetab.com/docs/recipes/vite-react/query/polling.md)
- [Vite · React · TanStack Query · Server-sent events](https://spinetab.com/docs/recipes/vite-react/query/sse.md)
- [Vite · React · TanStack Query · WebSocket](https://spinetab.com/docs/recipes/vite-react/query/websocket.md)
- [Vite · React · TanStack Query · Fetch stream](https://spinetab.com/docs/recipes/vite-react/query/stream.md)
- [Vite · React · TanStack Query · GraphQL over WebSocket](https://spinetab.com/docs/recipes/vite-react/query/graphql-ws.md)
- [Vite · React · TanStack Query · GraphQL over SSE](https://spinetab.com/docs/recipes/vite-react/query/graphql-sse.md)
- [Vite · React · TanStack Query · Socket.IO](https://spinetab.com/docs/recipes/vite-react/query/socket-io.md)

HTTP polling · [Vite · React](https://spinetab.com/docs/setup/vite.md)

## Before you start

Start with an existing Vite · React app. Keep its renderer, routes and plugins. These files add one live view; Spinetab does not create your API.

Your endpoint supplies complete `{ "open": 12 }` values: JSON from GET /api/queue, read every five seconds by default.

The examples use same-origin URLs and declare `anonymous: true`: no token is supplied, although cookies still flow. For a separate API origin, use its URL and configure CORS or your application's proxy. For private data, add [credentials and user scopes](https://spinetab.com/docs/concepts/credentials.md); never put a secret in these files.

## 1\. Install

```sh
pnpm add spinetab @tanstack/query-core@^5.104.0 @tanstack/react-query@^5.104.0
```

Keep your framework's existing dependencies. See [compatible versions](https://spinetab.com/docs/compatibility.md) if upgrading an older app.

## 2\. Configure your app

Merge this addition into your configuration; preserve existing plugins and options. Restart the dev server afterwards. The plugin bundles the worker and discovers the adapters imported below.

**File: `vite.config.ts`**

```ts
import react from "@vitejs/plugin-react";
import { spinetab } from "spinetab/vite";
import { defineConfig } from "vite";

export default defineConfig({ plugins: [react(), spinetab()] });
```

## 3\. Connect and render

Save these files together in `src/recipe/`.

The provider owns a fresh QueryClient. If your app already has a provider, render `Queue` beneath it instead of creating another one. The query uses `skipToken` to read live cache updates without fetching a competing snapshot. The bridge uses that same client's `["queue"]` key.

`reconcile: "latest"` fits this full-state contract: the next complete event restores the displayed value after a gap. Use a [refresh/merge policy](https://spinetab.com/docs/concepts/continuity.md) for deltas or partial GraphQL results. Reconnecting alone does not reconstruct missed state.

**File: `src/recipe/live.ts`**

```ts
import { createSpinetab } from "spinetab";

export const spinetab = createSpinetab({ anonymous: true });
```

**File: `src/recipe/queue-types.ts`**

```ts
export type Queue = { open: number };
export type QueueView = { open?: number; problem?: string };
```

**File: `src/recipe/source.ts`**

```ts
import { polling } from "spinetab/polling";
import type { Queue, QueueView } from "./queue-types";

export const queueSource = polling<Queue>("/api/queue");
export const selectQueue = (queue: Queue): QueueView => queue;
```

**File: `src/recipe/feed.ts`**

```ts
import type { QueryClient } from "@tanstack/query-core";
import { bindQuery } from "spinetab/tanstack-query";
import { spinetab } from "./live";
import { queueSource, selectQueue } from "./source";

export function startQueue(
	queryClient: QueryClient,
	onError: (message: string) => void,
) {
	return bindQuery(spinetab, queueSource, {
		queryClient,
		queryKey: ["queue"],
		map: selectQueue,
		reconcile: "latest",
		onError: (error) => onError(error.message),
		onEvent: () => onError(""),
	});
}
```

**File: `src/recipe/Queue.tsx`**

```tsx
import { skipToken, useQuery, useQueryClient } from "@tanstack/react-query";
import { useEffect, useState } from "react";
import { startQueue } from "./feed";
import type { QueueView } from "./queue-types";

export default function Queue() {
	const client = useQueryClient();
	const { data } = useQuery<QueueView>({
		queryKey: ["queue"],
		queryFn: skipToken,
	});
	const [error, setError] = useState("");
	useEffect(() => {
		const binding = startQueue(client, setError);
		return () => binding.unsubscribe();
	}, [client]);
	if (error || data?.problem)
		return <p role="alert">{error || data?.problem}</p>;
	if (data?.open === undefined) return <p role="status">Loading queue…</p>;
	return (
		<p>
			<output>{data.open}</output> open
		</p>
	);
}
```

**File: `src/recipe/Recipe.tsx`**

```tsx
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { useState } from "react";
import Queue from "./Queue";

export default function Recipe() {
	const [client] = useState(() => new QueryClient());
	return (
		<QueryClientProvider client={client}>
			<Queue />
		</QueryClientProvider>
	);
}
```

## 4\. Mount the view

**File: `src/App.tsx`**

```tsx
import Recipe from "./recipe/Recipe";
export default function App() {
	return <Recipe />;
}
```

Your existing entry point mounts App as usual. Keep its renderer plugin; no additional application provider is required beyond those shown.

## Check the result

Open the page in two tabs of the same browser profile. Both should show advancing queue values. Matching subscriptions share upstream work when SharedWorker is available; fallback runs independently in each tab. Remove one view and the other should keep updating. Removing the last view releases its subscription; connection closure can follow the adapter's idle delay.

[API options and advanced recovery](https://spinetab.com/docs/integrations/tanstack-query.md) · [Authentication](https://spinetab.com/docs/concepts/credentials.md)
