---
title: "tRPC recipes"
url: "https://spinetab.com/docs/recipes/trpc/"
description: "tRPC over WebSocket with tRPC in Vite · React: dependencies, configuration, component and cleanup."
---

# tRPC recipes

Subscribe to a typed procedure over WebSocket or SSE.

**Available recipes**

- [Vite · React · tRPC · tRPC over WebSocket](https://spinetab.com/docs/recipes/vite-react/trpc/trpc-ws.md)
- [Vite · React · tRPC · tRPC over SSE](https://spinetab.com/docs/recipes/vite-react/trpc/trpc-sse.md)
- [Vite · Vue · tRPC · tRPC over WebSocket](https://spinetab.com/docs/recipes/vite-vue/trpc/trpc-ws.md)
- [Vite · Vue · tRPC · tRPC over SSE](https://spinetab.com/docs/recipes/vite-vue/trpc/trpc-sse.md)
- [Vite · Svelte · tRPC · tRPC over WebSocket](https://spinetab.com/docs/recipes/vite-svelte/trpc/trpc-ws.md)
- [Vite · Svelte · tRPC · tRPC over SSE](https://spinetab.com/docs/recipes/vite-svelte/trpc/trpc-sse.md)
- [Vite · Solid · tRPC · tRPC over WebSocket](https://spinetab.com/docs/recipes/vite-solid/trpc/trpc-ws.md)
- [Vite · Solid · tRPC · tRPC over SSE](https://spinetab.com/docs/recipes/vite-solid/trpc/trpc-sse.md)
- [Next.js · tRPC · tRPC over WebSocket](https://spinetab.com/docs/recipes/nextjs/trpc/trpc-ws.md)
- [Next.js · tRPC · tRPC over SSE](https://spinetab.com/docs/recipes/nextjs/trpc/trpc-sse.md)
- [Astro · React · tRPC · tRPC over WebSocket](https://spinetab.com/docs/recipes/astro-react/trpc/trpc-ws.md)
- [Astro · React · tRPC · tRPC over SSE](https://spinetab.com/docs/recipes/astro-react/trpc/trpc-sse.md)
- [Astro · Vue · tRPC · tRPC over WebSocket](https://spinetab.com/docs/recipes/astro-vue/trpc/trpc-ws.md)
- [Astro · Vue · tRPC · tRPC over SSE](https://spinetab.com/docs/recipes/astro-vue/trpc/trpc-sse.md)
- [Astro · Svelte · tRPC · tRPC over WebSocket](https://spinetab.com/docs/recipes/astro-svelte/trpc/trpc-ws.md)
- [Astro · Svelte · tRPC · tRPC over SSE](https://spinetab.com/docs/recipes/astro-svelte/trpc/trpc-sse.md)
- [Astro · Solid · tRPC · tRPC over WebSocket](https://spinetab.com/docs/recipes/astro-solid/trpc/trpc-ws.md)
- [Astro · Solid · tRPC · tRPC over SSE](https://spinetab.com/docs/recipes/astro-solid/trpc/trpc-sse.md)
- [Nuxt · tRPC · tRPC over WebSocket](https://spinetab.com/docs/recipes/nuxt/trpc/trpc-ws.md)
- [Nuxt · tRPC · tRPC over SSE](https://spinetab.com/docs/recipes/nuxt/trpc/trpc-sse.md)
- [SvelteKit · tRPC · tRPC over WebSocket](https://spinetab.com/docs/recipes/sveltekit/trpc/trpc-ws.md)
- [SvelteKit · tRPC · tRPC over SSE](https://spinetab.com/docs/recipes/sveltekit/trpc/trpc-sse.md)
- [React Router · tRPC · tRPC over WebSocket](https://spinetab.com/docs/recipes/react-router/trpc/trpc-ws.md)
- [React Router · tRPC · tRPC over SSE](https://spinetab.com/docs/recipes/react-router/trpc/trpc-sse.md)

tRPC over WebSocket · [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.

Use a tRPC 11 server with a `queue` subscription returning a complete `{ open: number }` value. The server router below defines that contract; mount it at `/trpc-ws` with tRPC's WebSocket adapter. The browser imports only its type. This standalone client recipe uses untracked full-state results; [tracked events and transformers](https://spinetab.com/docs/integrations/trpc.md) require their corresponding result handling.

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 @trpc/client@^11.19.0 @trpc/server@^11.19.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/`.

`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/router.ts`**

```ts
import { initTRPC } from "@trpc/server";

const t = initTRPC.create();

// Server-side contract. Mount this router on your existing tRPC server.
export const appRouter = t.router({
	queue: t.procedure.subscription(async function* ({ signal }) {
		let open = 0;
		while (!signal?.aborted) {
			yield { open: open++ };
			await new Promise<void>((resolve) => {
				const finish = () => {
					clearTimeout(timer);
					signal?.removeEventListener("abort", finish);
					resolve();
				};
				const timer = setTimeout(finish, 1_000);
				signal?.addEventListener("abort", finish, { once: true });
			});
		}
	}),
});
export type AppRouter = typeof appRouter;
```

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

```ts
import { createTRPCClient, httpBatchLink, splitLink } from "@trpc/client";
import { spinetabWsLink } from "spinetab/trpc";
import { spinetab } from "./live";
import type { QueueView } from "./queue-types";
import type { AppRouter } from "./router";

const trpc = createTRPCClient<AppRouter>({
	links: [
		splitLink({
			condition: (operation) => operation.type === "subscription",
			true: spinetabWsLink<AppRouter>({
				client: spinetab,
				url: "/trpc-ws",
				reconcile: "latest",
			}),
			false: httpBatchLink({ url: "/trpc" }),
		}),
	],
});

export function watchQueue(
	next: (value: QueueView) => void,
	fail: (message: string) => void,
) {
	const subscription = trpc.queue.subscribe(undefined, {
		onData: (value) => {
			fail("");
			next(value);
		},
		onConnectionStateChange: (state) => {
			if (state.error) fail(state.error.message);
		},
		onError: (error) => fail(error.message),
	});
	return () => subscription.unsubscribe();
}
```

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

```tsx
import { useEffect, useState } from "react";
import type { QueueView } from "./queue-types";
import { watchQueue } from "./watch";

export default function Recipe() {
	const [data, setData] = useState<QueueView>();
	const [error, setError] = useState("");
	useEffect(
		() =>
			watchQueue((value) => {
				setData(value);
				setError("");
			}, setError),
		[],
	);
	if (error) return <p role="alert">{error}</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: `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/trpc.md) · [Authentication](https://spinetab.com/docs/concepts/credentials.md)
