---
title: "Next.js · Component state · HTTP polling"
url: "https://spinetab.com/docs/recipes/nextjs/direct/polling/"
description: "HTTP polling with Component state in Next.js: dependencies, configuration, component and cleanup."
---

# Next.js · Component state · HTTP polling

Render a live feed with your framework's component binding.

**Available recipes**

- [Next.js · Component state · HTTP polling](https://spinetab.com/docs/recipes/nextjs/direct/polling.md)
- [Next.js · Component state · Server-sent events](https://spinetab.com/docs/recipes/nextjs/direct/sse.md)
- [Next.js · Component state · WebSocket](https://spinetab.com/docs/recipes/nextjs/direct/websocket.md)
- [Next.js · Component state · Fetch stream](https://spinetab.com/docs/recipes/nextjs/direct/stream.md)
- [Next.js · Component state · GraphQL over WebSocket](https://spinetab.com/docs/recipes/nextjs/direct/graphql-ws.md)
- [Next.js · Component state · GraphQL over SSE](https://spinetab.com/docs/recipes/nextjs/direct/graphql-sse.md)
- [Next.js · Component state · Socket.IO](https://spinetab.com/docs/recipes/nextjs/direct/socket-io.md)

HTTP polling · [Next.js](https://spinetab.com/docs/setup/nextjs.md)

## Before you start

Start with an existing Next.js 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
```

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: `next.config.ts`**

```ts
import type { NextConfig } from "next";
import { withSpinetab } from "spinetab/next";

const config: NextConfig = {};
export default withSpinetab(config);
```

## 3\. Connect and render

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

`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: `app/recipe/live.ts`**

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

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

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

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

**File: `app/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: `app/recipe/Recipe.tsx`**

```tsx
import { bindClient } from "spinetab/react";
import { spinetab } from "./live";
import { queueSource, selectQueue } from "./source";

const { useLive } = bindClient(spinetab);

export default function Recipe() {
	const { data, error } = useLive(queueSource, {
		map: selectQueue,
		reconcile: "latest",
	});
	if (error || data?.problem)
		return <p role="alert">{error?.message ?? data?.problem}</p>;
	if (data?.open === undefined) return <p role="status">Loading queue…</p>;
	return (
		<p>
			<output>{data.open}</output> open
		</p>
	);
}
```

## 4\. Mount the view

**File: `app/Live.tsx`**

```tsx
"use client";

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

**File: `app/page.tsx`**

```tsx
import Live from "./Live";
export default function Page() {
	return (
		<main>
			<Live />
		</main>
	);
}
```

The `"use client"` boundary includes the view and its imports. Keep subscriptions out of Server Components, loaders and server actions. Retain your existing root layout; use `src/app/` instead if that is your app directory. Both supported Next bundlers use this configuration.

## 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/transports/polling.md) · [Authentication](https://spinetab.com/docs/concepts/credentials.md)
