Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

streaming-data-from-server-functions.md

97 lines2.7 KB

1---
2title: Streaming Data from Server Functions
3---
4​
5Streaming 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​
7The two most popular ways of streaming data from server functions are using `ReadableStream`-s or async generators.
8​
9You 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​
13Here's an example for a server function that streams an array of messages to the client in a type-safe manner:
14​
15```ts
16type Message = {
17 content: string
19​
20/**
21 This server function returns a `ReadableStream`
22 that streams `Message` chunks to the client.
23*/
24const streamingResponseFn = createServerFn().handler(async () => {
25 // These are the messages that you want to send as chunks to the client
26 const messages: Message[] = generateMessages()
27​
28 // This `ReadableStream` is typed, so each
29 // will be of type `Message`.
30 const stream = new ReadableStream<Message>({
31 async start(controller) {
32 for (const message of messages) {
33 // Send the message
34 controller.enqueue(message)
35 }
36 controller.close()
37 },
38 })
39​
40 return stream
41})
42```
43​
44When you consume this stream from the client, the streamed chunks will be properly typed:
45​
46```ts
47const [message, setMessage] = useState('')
48​
49const getTypedReadableStreamResponse = useCallback(async () => {
50 const response = await streamingResponseFn()
51​
52 if (!response) {
53 return
54 }
55​
56 const reader = response.getReader()
57 let done = false
58 while (!done) {
59 const { value, done: doneReading } = await reader.read()
60 done = doneReading
61 if (value) {
62 // Notice how we know the value of `chunk` (`Message | undefined`)
63 // here, because it's coming from the typed `ReadableStream`
64 const chunk = value.content
65 setMessage((prev) => prev + chunk)
66 }
67 }
68}, [])
69```
70​
71## Async Generators in Server Functions
72​
73A much cleaner approach with the same results is to use an async generator function:
74​
75```ts
76const streamingWithAnAsyncGeneratorFn = createServerFn().handler(
77 async function* () {
78 const messages: Message[] = generateMessages()
79 for (const msg of messages) {
80 await sleep(500)
81 // The streamed chunks are still typed as `Message`
82 yield msg
83 }
84 },
86```
87​
88The client side code will also be leaner:
89​
90```ts
91const getResponseFromTheAsyncGenerator = useCallback(async () => {
92 for await (const msg of await streamingWithAnAsyncGeneratorFn()) {
93 const chunk = msg.content
94 setMessages((prev) => prev + chunk)
95 }
96}, [])
97```