@trpc/server 11.14.0 → 11.14.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,406 @@
1
+ ---
2
+ name: subscriptions
3
+ description: >
4
+ Set up real-time event streams with async generator subscriptions using
5
+ .subscription(async function*() { yield }). SSE via httpSubscriptionLink is
6
+ recommended over WebSocket. Use tracked(id, data) from @trpc/server for
7
+ reconnection recovery with lastEventId. WebSocket via wsLink and
8
+ createWSClient from @trpc/client, applyWSSHandler from @trpc/server/adapters/ws. Configure SSE ping with
9
+ initTRPC.create({ sse: { ping: { enabled, intervalMs } } }). AbortSignal
10
+ via opts.signal for cleanup. splitLink to route subscriptions.
11
+ type: core
12
+ library: trpc
13
+ library_version: '11.14.0'
14
+ requires:
15
+ - server-setup
16
+ - links
17
+ sources:
18
+ - www/docs/server/subscriptions.md
19
+ - www/docs/server/websockets.md
20
+ - www/docs/client/links/httpSubscriptionLink.md
21
+ - www/docs/client/links/wsLink.md
22
+ - packages/server/src/unstable-core-do-not-import/stream/sse.ts
23
+ - packages/server/src/unstable-core-do-not-import/stream/tracked.ts
24
+ - examples/standalone-server/src/server.ts
25
+ ---
26
+
27
+ # tRPC — Subscriptions
28
+
29
+ ## Setup
30
+
31
+ SSE is recommended for most subscription use cases. It is simpler to set up and does not require a WebSocket server.
32
+
33
+ ### Server
34
+
35
+ ```ts
36
+ // server.ts
37
+ import EventEmitter, { on } from 'node:events';
38
+ import { initTRPC, tracked } from '@trpc/server';
39
+ import { createHTTPServer } from '@trpc/server/adapters/standalone';
40
+ import { z } from 'zod';
41
+
42
+ const t = initTRPC.create({
43
+ sse: {
44
+ ping: {
45
+ enabled: true,
46
+ intervalMs: 2000,
47
+ },
48
+ client: {
49
+ reconnectAfterInactivityMs: 5000,
50
+ },
51
+ },
52
+ });
53
+
54
+ type Post = { id: string; title: string };
55
+ const ee = new EventEmitter();
56
+
57
+ const appRouter = t.router({
58
+ onPostAdd: t.procedure
59
+ .input(z.object({ lastEventId: z.string().nullish() }).optional())
60
+ .subscription(async function* (opts) {
61
+ for await (const [data] of on(ee, 'add', { signal: opts.signal })) {
62
+ const post = data as Post;
63
+ yield tracked(post.id, post);
64
+ }
65
+ }),
66
+ });
67
+
68
+ export type AppRouter = typeof appRouter;
69
+
70
+ createHTTPServer({
71
+ router: appRouter,
72
+ createContext() {
73
+ return {};
74
+ },
75
+ }).listen(3000);
76
+ ```
77
+
78
+ ### Client (SSE)
79
+
80
+ ```ts
81
+ // client.ts
82
+ import {
83
+ createTRPCClient,
84
+ httpBatchLink,
85
+ httpSubscriptionLink,
86
+ splitLink,
87
+ } from '@trpc/client';
88
+ import type { AppRouter } from './server';
89
+
90
+ const trpc = createTRPCClient<AppRouter>({
91
+ links: [
92
+ splitLink({
93
+ condition: (op) => op.type === 'subscription',
94
+ true: httpSubscriptionLink({ url: 'http://localhost:3000' }),
95
+ false: httpBatchLink({ url: 'http://localhost:3000' }),
96
+ }),
97
+ ],
98
+ });
99
+
100
+ const subscription = trpc.onPostAdd.subscribe(
101
+ { lastEventId: null },
102
+ {
103
+ onData(post) {
104
+ console.log('New post:', post);
105
+ },
106
+ onError(err) {
107
+ console.error('Subscription error:', err);
108
+ },
109
+ },
110
+ );
111
+
112
+ // To stop:
113
+ // subscription.unsubscribe();
114
+ ```
115
+
116
+ ## Core Patterns
117
+
118
+ ### tracked() for reconnection recovery
119
+
120
+ ```ts
121
+ import EventEmitter, { on } from 'node:events';
122
+ import { initTRPC, tracked } from '@trpc/server';
123
+ import { z } from 'zod';
124
+
125
+ const t = initTRPC.create();
126
+ const ee = new EventEmitter();
127
+
128
+ const appRouter = t.router({
129
+ onPostAdd: t.procedure
130
+ .input(z.object({ lastEventId: z.string().nullish() }).optional())
131
+ .subscription(async function* (opts) {
132
+ const iterable = on(ee, 'add', { signal: opts.signal });
133
+
134
+ if (opts.input?.lastEventId) {
135
+ // Fetch and yield events since lastEventId from your database
136
+ // const missed = await db.post.findMany({ where: { id: { gt: opts.input.lastEventId } } });
137
+ // for (const post of missed) { yield tracked(post.id, post); }
138
+ }
139
+
140
+ for await (const [data] of iterable) {
141
+ yield tracked(data.id, data);
142
+ }
143
+ }),
144
+ });
145
+ ```
146
+
147
+ When using `tracked(id, data)`, the client automatically sends `lastEventId` on reconnection. For SSE this is part of the EventSource spec; for WebSocket, `wsLink` handles it.
148
+
149
+ ### Polling loop subscription
150
+
151
+ ```ts
152
+ import { initTRPC, tracked } from '@trpc/server';
153
+ import { z } from 'zod';
154
+
155
+ const t = initTRPC.create();
156
+
157
+ const appRouter = t.router({
158
+ onNewItems: t.procedure
159
+ .input(z.object({ lastEventId: z.coerce.date().nullish() }))
160
+ .subscription(async function* (opts) {
161
+ let cursor = opts.input?.lastEventId ?? null;
162
+
163
+ while (!opts.signal?.aborted) {
164
+ const items = await db.item.findMany({
165
+ where: cursor ? { createdAt: { gt: cursor } } : undefined,
166
+ orderBy: { createdAt: 'asc' },
167
+ });
168
+
169
+ for (const item of items) {
170
+ yield tracked(item.createdAt.toJSON(), item);
171
+ cursor = item.createdAt;
172
+ }
173
+
174
+ await new Promise((r) => setTimeout(r, 1000));
175
+ }
176
+ }),
177
+ });
178
+ ```
179
+
180
+ ### WebSocket setup (when bidirectional communication is required)
181
+
182
+ ```ts
183
+ // server
184
+ import { applyWSSHandler } from '@trpc/server/adapters/ws';
185
+ import { WebSocketServer } from 'ws';
186
+ import { appRouter } from './router';
187
+
188
+ const wss = new WebSocketServer({ port: 3001 });
189
+ const handler = applyWSSHandler({
190
+ wss,
191
+ router: appRouter,
192
+ createContext() {
193
+ return {};
194
+ },
195
+ keepAlive: {
196
+ enabled: true,
197
+ pingMs: 30000,
198
+ pongWaitMs: 5000,
199
+ },
200
+ });
201
+
202
+ process.on('SIGTERM', () => {
203
+ handler.broadcastReconnectNotification();
204
+ wss.close();
205
+ });
206
+ ```
207
+
208
+ ```ts
209
+ // client
210
+ import {
211
+ createTRPCClient,
212
+ createWSClient,
213
+ httpBatchLink,
214
+ splitLink,
215
+ wsLink,
216
+ } from '@trpc/client';
217
+ import type { AppRouter } from './server';
218
+
219
+ const wsClient = createWSClient({ url: 'ws://localhost:3001' });
220
+
221
+ const trpc = createTRPCClient<AppRouter>({
222
+ links: [
223
+ splitLink({
224
+ condition: (op) => op.type === 'subscription',
225
+ true: wsLink({ client: wsClient }),
226
+ false: httpBatchLink({ url: 'http://localhost:3000' }),
227
+ }),
228
+ ],
229
+ });
230
+ ```
231
+
232
+ ### Cleanup with try...finally
233
+
234
+ ```ts
235
+ const appRouter = t.router({
236
+ events: t.procedure.subscription(async function* (opts) {
237
+ const cleanup = registerListener();
238
+ try {
239
+ for await (const [data] of on(ee, 'event', { signal: opts.signal })) {
240
+ yield data;
241
+ }
242
+ } finally {
243
+ cleanup();
244
+ }
245
+ }),
246
+ });
247
+ ```
248
+
249
+ tRPC invokes `.return()` on the generator when the subscription stops, triggering the `finally` block.
250
+
251
+ ## Common Mistakes
252
+
253
+ ### HIGH Using Observable instead of async generator
254
+
255
+ Wrong:
256
+
257
+ ```ts
258
+ import { observable } from '@trpc/server/observable';
259
+
260
+ t.procedure.subscription(({ input }) => {
261
+ return observable((emit) => {
262
+ emit.next(data);
263
+ });
264
+ });
265
+ ```
266
+
267
+ Correct:
268
+
269
+ ```ts
270
+ t.procedure.subscription(async function* ({ input, signal }) {
271
+ for await (const [data] of on(ee, 'event', { signal })) {
272
+ yield data;
273
+ }
274
+ });
275
+ ```
276
+
277
+ Observable subscriptions are deprecated and will be removed in v12. Use async generator syntax (`async function*`).
278
+
279
+ Source: packages/server/src/unstable-core-do-not-import/procedureBuilder.ts
280
+
281
+ ### MEDIUM Empty string as tracked event ID
282
+
283
+ Wrong:
284
+
285
+ ```ts
286
+ yield tracked('', data);
287
+ ```
288
+
289
+ Correct:
290
+
291
+ ```ts
292
+ yield tracked(event.id.toString(), data);
293
+ ```
294
+
295
+ `tracked()` throws if the ID is an empty string because it conflicts with SSE "no id" semantics.
296
+
297
+ Source: packages/server/src/unstable-core-do-not-import/stream/tracked.ts
298
+
299
+ ### HIGH Fetching history before setting up event listener
300
+
301
+ Wrong:
302
+
303
+ ```ts
304
+ t.procedure.subscription(async function* (opts) {
305
+ const history = await db.getEvents(); // events may fire here and be lost
306
+ yield* history;
307
+ for await (const event of listener) {
308
+ yield event;
309
+ }
310
+ });
311
+ ```
312
+
313
+ Correct:
314
+
315
+ ```ts
316
+ t.procedure.subscription(async function* (opts) {
317
+ const iterable = on(ee, 'event', { signal: opts.signal }); // listen first
318
+ const history = await db.getEvents();
319
+ for (const item of history) {
320
+ yield tracked(item.id, item);
321
+ }
322
+ for await (const [event] of iterable) {
323
+ yield tracked(event.id, event);
324
+ }
325
+ });
326
+ ```
327
+
328
+ If you fetch historical data before setting up the event listener, events emitted between the fetch and listener setup are lost.
329
+
330
+ Source: www/docs/server/subscriptions.md
331
+
332
+ ### MEDIUM SSE ping interval >= client reconnect interval
333
+
334
+ Wrong:
335
+
336
+ ```ts
337
+ initTRPC.create({
338
+ sse: {
339
+ ping: { enabled: true, intervalMs: 10000 },
340
+ client: { reconnectAfterInactivityMs: 5000 },
341
+ },
342
+ });
343
+ ```
344
+
345
+ Correct:
346
+
347
+ ```ts
348
+ initTRPC.create({
349
+ sse: {
350
+ ping: { enabled: true, intervalMs: 2000 },
351
+ client: { reconnectAfterInactivityMs: 5000 },
352
+ },
353
+ });
354
+ ```
355
+
356
+ If the server ping interval is >= the client reconnect timeout, the client disconnects thinking the connection is dead before receiving a ping.
357
+
358
+ Source: packages/server/src/unstable-core-do-not-import/stream/sse.ts
359
+
360
+ ### HIGH Sending custom headers with SSE without EventSource polyfill
361
+
362
+ Wrong:
363
+
364
+ ```ts
365
+ httpSubscriptionLink({
366
+ url: 'http://localhost:3000',
367
+ // Native EventSource does not support custom headers
368
+ });
369
+ ```
370
+
371
+ Correct:
372
+
373
+ ```ts
374
+ import { EventSourcePolyfill } from 'event-source-polyfill';
375
+
376
+ httpSubscriptionLink({
377
+ url: 'http://localhost:3000',
378
+ EventSource: EventSourcePolyfill,
379
+ eventSourceOptions: async () => ({
380
+ headers: { authorization: 'Bearer token' },
381
+ }),
382
+ });
383
+ ```
384
+
385
+ The native EventSource API does not support custom headers. Use an EventSource polyfill and pass it via the `EventSource` option on `httpSubscriptionLink`.
386
+
387
+ Source: www/docs/client/links/httpSubscriptionLink.md
388
+
389
+ ### MEDIUM Choosing WebSocket when SSE would suffice
390
+
391
+ SSE (`httpSubscriptionLink`) is recommended for most subscription use cases. WebSockets add complexity (connection management, reconnection, keepalive, separate server process). Only use `wsLink` when bidirectional communication or WebSocket-specific features are required.
392
+
393
+ Source: maintainer interview
394
+
395
+ ### MEDIUM WebSocket subscription stale inputs on reconnect
396
+
397
+ When a WebSocket reconnects, subscriptions re-send the original input parameters. There is no hook to re-evaluate inputs on reconnect, which can cause stale data. Consider using `tracked()` with `lastEventId` to mitigate this.
398
+
399
+ Source: https://github.com/trpc/trpc/issues/4122
400
+
401
+ ## See Also
402
+
403
+ - **links** -- `splitLink`, `httpSubscriptionLink`, `wsLink`, `httpBatchLink`
404
+ - **auth** -- authenticating subscription connections (connectionParams, cookies, EventSource polyfill headers)
405
+ - **server-setup** -- `initTRPC.create()` SSE configuration options
406
+ - **adapter-fastify** -- WebSocket subscriptions via `@fastify/websocket` and `useWSS`
@@ -0,0 +1,151 @@
1
+ ---
2
+ name: trpc-router
3
+ description: >
4
+ Entry point for all tRPC skills. Decision tree routing by task: initTRPC.create(),
5
+ t.router(), t.procedure, createTRPCClient, adapters, subscriptions, React Query,
6
+ Next.js, links, middleware, validators, error handling, caching, FormData.
7
+ type: core
8
+ library: trpc
9
+ library_version: '11.14.0'
10
+ requires: []
11
+ sources:
12
+ - 'trpc/trpc:www/docs/main/introduction.mdx'
13
+ - 'trpc/trpc:www/docs/main/quickstart.mdx'
14
+ ---
15
+
16
+ # tRPC -- Skill Router
17
+
18
+ ## Decision Tree
19
+
20
+ ### What are you trying to do?
21
+
22
+ #### Define a tRPC backend (server)
23
+
24
+ - **Initialize tRPC, define routers, procedures, context, export AppRouter**
25
+ -> Load skill: `server-setup`
26
+
27
+ - **Add middleware (.use), auth guards, logging, base procedures**
28
+ -> Load skill: `middlewares`
29
+
30
+ - **Add input/output validation with Zod or other libraries**
31
+ -> Load skill: `validators`
32
+
33
+ - **Throw typed errors, format errors for clients, global error handling**
34
+ -> Load skill: `error-handling`
35
+
36
+ - **Call procedures from server code, write integration tests**
37
+ -> Load skill: `server-side-calls`
38
+
39
+ - **Set Cache-Control headers on query responses (CDN, browser caching)**
40
+ -> Load skill: `caching`
41
+
42
+ - **Accept FormData, File, Blob, or binary uploads in mutations**
43
+ -> Load skill: `non-json-content-types`
44
+
45
+ - **Set up real-time subscriptions (SSE or WebSocket)**
46
+ -> Load skill: `subscriptions`
47
+
48
+ #### Host the tRPC API (adapters)
49
+
50
+ - **Node.js built-in HTTP server (simplest, local dev)**
51
+ -> Load skill: `adapter-standalone`
52
+
53
+ - **Express middleware**
54
+ -> Load skill: `adapter-express`
55
+
56
+ - **Fastify plugin**
57
+ -> Load skill: `adapter-fastify`
58
+
59
+ - **AWS Lambda (API Gateway v1/v2, Function URLs)**
60
+ -> Load skill: `adapter-aws-lambda`
61
+
62
+ - **Fetch API / Edge (Cloudflare Workers, Deno, Vercel Edge, Astro, Remix)**
63
+ -> Load skill: `adapter-fetch`
64
+
65
+ #### Consume the tRPC API (client)
66
+
67
+ - **Create a vanilla TypeScript client, configure links, headers, types**
68
+ -> Load skill: `client-setup`
69
+
70
+ - **Configure link chain (batching, streaming, splitting, WebSocket, SSE)**
71
+ -> Load skill: `links`
72
+
73
+ - **Use SuperJSON transformer for Date, Map, Set, BigInt**
74
+ -> Load skill: `superjson`
75
+
76
+ #### Use tRPC with a framework
77
+
78
+ - **React with TanStack Query (useQuery, useMutation, queryOptions)**
79
+ -> Load skill: `react-query-setup`
80
+
81
+ - **Next.js App Router (RSC, server components, HydrateClient)**
82
+ -> Load skill: `nextjs-app-router`
83
+
84
+ - **Next.js Pages Router (withTRPC, SSR, SSG helpers)**
85
+ -> Load skill: `nextjs-pages-router`
86
+
87
+ #### Advanced patterns
88
+
89
+ - **Generate OpenAPI spec, REST client from tRPC router**
90
+ -> Load skill: `openapi`
91
+
92
+ - **Multi-service gateway, custom routing links, SOA**
93
+ -> Load skill: `service-oriented-architecture`
94
+
95
+ - **Auth middleware + client headers + subscription auth**
96
+ -> Load skill: `auth`
97
+
98
+ ## Quick Reference: Minimal Working App
99
+
100
+ ```ts
101
+ // server/trpc.ts
102
+ import { initTRPC } from '@trpc/server';
103
+
104
+ const t = initTRPC.create();
105
+
106
+ export const router = t.router;
107
+ export const publicProcedure = t.procedure;
108
+ ```
109
+
110
+ ```ts
111
+ // server/appRouter.ts
112
+ import { z } from 'zod';
113
+ import { publicProcedure, router } from './trpc';
114
+
115
+ export const appRouter = router({
116
+ hello: publicProcedure
117
+ .input(z.object({ name: z.string() }))
118
+ .query(({ input }) => ({ greeting: `Hello ${input.name}` })),
119
+ });
120
+
121
+ export type AppRouter = typeof appRouter;
122
+ ```
123
+
124
+ ```ts
125
+ // server/index.ts
126
+ import { createHTTPServer } from '@trpc/server/adapters/standalone';
127
+ import { appRouter } from './appRouter';
128
+
129
+ const server = createHTTPServer({ router: appRouter });
130
+ server.listen(3000);
131
+ ```
132
+
133
+ ```ts
134
+ // client/index.ts
135
+ import { createTRPCClient, httpBatchLink } from '@trpc/client';
136
+ import type { AppRouter } from '../server/appRouter';
137
+
138
+ const trpc = createTRPCClient<AppRouter>({
139
+ links: [httpBatchLink({ url: 'http://localhost:3000' })],
140
+ });
141
+
142
+ const result = await trpc.hello.query({ name: 'World' });
143
+ ```
144
+
145
+ ## See Also
146
+
147
+ - `server-setup` -- full server initialization details
148
+ - `client-setup` -- full client configuration
149
+ - `adapter-standalone` -- simplest adapter for getting started
150
+ - `react-query-setup` -- React integration
151
+ - `nextjs-app-router` -- Next.js App Router integration