Open History to see the last commit
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
18}
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 },
85)
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```