Skip to content

Commit 188e6a8

Browse files
authored
feat(server + client): streaming mutations and queries over HTTP (#5700)
1 parent 8cc33e2 commit 188e6a8

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

src/client/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* This is the client-side code that uses the inferred types from the server
33
*/
4-
import { createTRPCClient, httpBatchLink } from '@trpc/client';
4+
import { createTRPCClient, unstable_httpBatchStreamLink } from '@trpc/client';
55
/**
66
* We only import the `AppRouter` type from the server - this is not available at runtime
77
*/
@@ -10,7 +10,7 @@ import type { AppRouter } from '../server/index.js';
1010
// Initialize the tRPC client
1111
const trpc = createTRPCClient<AppRouter>({
1212
links: [
13-
httpBatchLink({
13+
unstable_httpBatchStreamLink({
1414
url: 'http://localhost:3000',
1515
}),
1616
],
@@ -34,3 +34,9 @@ console.log('Created user:', createdUser);
3434
const user = await trpc.user.byId.query('1');
3535
// ^?
3636
console.log('User 1:', user);
37+
38+
const iterable = await trpc.examples.iterable.query();
39+
40+
for await (const i of iterable) {
41+
console.log('Iterable:', i);
42+
}

src/server/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ const appRouter = router({
3232
return user;
3333
}),
3434
},
35+
examples: {
36+
iterable: publicProcedure.query(async function* () {
37+
for (let i = 0; i < 3; i++) {
38+
await new Promise((resolve) => setTimeout(resolve, 500));
39+
yield i;
40+
}
41+
}),
42+
},
3543
});
3644

3745
// Export type router type signature, this is used by the client.

src/server/trpc.ts

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

913
/**
1014
* Export reusable router and procedure helpers

0 commit comments

Comments
 (0)