Skip to content

Commit ff28c57

Browse files
authored
feat(server + client): add support for server-sent events (SSE) (#5713)
1 parent ce9ba1b commit ff28c57

3 files changed

Lines changed: 16 additions & 8 deletions

File tree

src/client/index.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/**
22
* This is the client-side code that uses the inferred types from the server
33
*/
4-
import { createTRPCClient, unstable_httpBatchStreamLink } from '@trpc/client';
4+
import {
5+
createTRPCClient,
6+
splitLink,
7+
unstable_httpBatchStreamLink,
8+
unstable_httpSubscriptionLink,
9+
} from '@trpc/client';
510
/**
611
* We only import the `AppRouter` type from the server - this is not available at runtime
712
*/
@@ -10,8 +15,14 @@ import type { AppRouter } from '../server/index.js';
1015
// Initialize the tRPC client
1116
const trpc = createTRPCClient<AppRouter>({
1217
links: [
13-
unstable_httpBatchStreamLink({
14-
url: 'http://localhost:3000',
18+
splitLink({
19+
condition: (op) => op.type === 'subscription',
20+
true: unstable_httpSubscriptionLink({
21+
url: 'http://localhost:3000',
22+
}),
23+
false: unstable_httpBatchStreamLink({
24+
url: 'http://localhost:3000',
25+
}),
1526
}),
1627
],
1728
});

src/server/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* This a minimal tRPC server
33
*/
44
import { createHTTPServer } from '@trpc/server/adapters/standalone';
5+
import { observable } from '@trpc/server/observable';
56
import { z } from 'zod';
67
import { db } from './db.js';
78
import { publicProcedure, router } from './trpc.js';

src/server/trpc.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ import { initTRPC } from '@trpc/server';
44
* Initialization of tRPC backend
55
* Should be done only once per backend!
66
*/
7-
const t = initTRPC.create({
8-
experimental: {
9-
iterablesAndDeferreds: true,
10-
},
11-
});
7+
const t = initTRPC.create();
128

139
/**
1410
* Export reusable router and procedure helpers

0 commit comments

Comments
 (0)