You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Streaming data from the server has become very popular thanks to the rise of AI apps. Luckily, it's a pretty easy task with TanStack Start, and what's even better: the streamed data is typed!
6
+
7
+
The two most popular ways of streaming data from server functions are using `ReadableStream`-s or async generators.
8
+
9
+
You can see how to implement both in the [Streaming Data From Server Functions example](https://github.com/TanStack/router/tree/main/examples/react/start-streaming-data-from-server-functions).
10
+
11
+
## Typed Readable Streams
12
+
13
+
Here's an example for a server function that streams an array of messages to the client in a type-safe manner:
14
+
15
+
```ts
16
+
typeMessage= {
17
+
content:string;
18
+
};
19
+
20
+
/**
21
+
This server function returns a `ReadableStream`
22
+
that streams `Message` chunks to the client.
23
+
*/
24
+
const streamingResponseFn =createServerFn({
25
+
method: "GET",
26
+
}).handler(async () => {
27
+
// These are the messages that you want to send as chunks to the client
28
+
const messages:Message[] =generateMessages();
29
+
30
+
// This `ReadableStream` is typed, so each
31
+
// will be of type `Message`.
32
+
const stream =newReadableStream<Message>({
33
+
async start(controller) {
34
+
for (const message ofmessages) {
35
+
// Send the message
36
+
controller.enqueue(message);
37
+
}
38
+
controller.close();
39
+
},
40
+
});
41
+
42
+
returnstream;
43
+
});
44
+
```
45
+
46
+
When you consume this stream from the client, the streamed chunks will be properly typed:
Copy file name to clipboardExpand all lines: docs/start/framework/react/server-functions.md
+57-53Lines changed: 57 additions & 53 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,15 +8,15 @@ title: Server Functions
8
8
Server functions let you define server-only logic that can be called from anywhere in your application - loaders, components, hooks, or other server functions. They run on the server but can be invoked from client code seamlessly.
// Call from anywhere - components, loaders, hooks, etc.
19
-
const time =awaitgetServerTime()
19
+
const time =awaitgetServerTime();
20
20
```
21
21
22
22
Server functions provide server capabilities (database access, environment variables, file system) while maintaining type safety across the network boundary.
@@ -26,18 +26,18 @@ Server functions provide server capabilities (database access, environment varia
26
26
Server functions are created with `createServerFn()` and can specify HTTP method:
-`setResponseHeader()` - Set custom response headers
205
205
-`setResponseStatus()` - Custom status codes
206
206
207
-
### Streaming & Raw Responses
207
+
### Streaming
208
+
209
+
Stream typed data from server functions to the client. See the [Streaming Data from Server Functions guide ](../guide/streaming-data-from-server-functions).
210
+
211
+
### Raw Responses
208
212
209
-
Return `Response` objects for streaming, binary data, or custom content types.
213
+
Return `Response` objects binary data, or custom content types.
0 commit comments