Streaming
Streaming allows you to send data from the server to the client progressively, improving perceived performance and user experience. TanStack Start supports multiple streaming strategies for both HTML rendering and server function responses.What is Streaming?
Streaming means:- Progressive rendering: Send HTML chunks as they’re ready
- Non-blocking: Don’t wait for slow operations
- Better UX: Show content immediately, load details later
- Efficient: Use server resources optimally
- Type-safe: Full TypeScript support for streamed data
Types of Streaming
TanStack Start supports two types of streaming:- HTML Streaming: Stream rendered HTML from server to browser
- Data Streaming: Stream data from server functions to client
HTML Streaming
Stream Handler
UsedefaultStreamHandler for HTML streaming:
- Begins sending HTML immediately
- Streams content as components render
- Handles deferred data automatically
- Injects hydration data
packages/start-server-core/src/createStartHandler.ts:353-359
Deferred Data with Streaming
Combine deferred data with streaming for optimal performance:examples/react/start-basic/src/routes/deferred.tsx:18-62
How HTML Streaming Works
Data Streaming
ReadableStream
Return aReadableStream from server functions:
examples/react/start-streaming-data-from-server-functions/src/routes/index.tsx:58-74
Async Generators
Use async generators for cleaner streaming code:examples/react/start-streaming-data-from-server-functions/src/routes/index.tsx:80-89
Streaming Protocol
TanStack Start uses multiple streaming protocols:NDJSON (Newline Delimited JSON)
For simple JSON chunks:packages/start-server-core/src/server-functions-handler.ts:301-308
Framed Protocol
For complex data with raw streams:- JSON-serializable data
- Binary streams (files, images)
- Multiple concurrent streams
packages/start-server-core/src/server-functions-handler.ts:241-278
Streaming Use Cases
1. AI/LLM Responses
Stream AI-generated text as it’s produced:2. Large Dataset Pagination
Stream large datasets in chunks:3. Real-time Progress Updates
Stream progress during long operations:4. Server-Sent Events (SSE) Alternative
Use streams as an alternative to SSE:Serialization
TanStack Start uses Seroval for streaming serialization:packages/start-server-core/src/server-functions-handler.ts:212-224
Error Handling in Streams
Handle Errors in Stream
Handle Errors on Client
Performance Considerations
1. Chunk Size
2. Backpressure
3. Connection Management
Best Practices
1. Use Appropriate Method
2. Provide Loading States
3. Handle Cleanup
Next Steps
- Learn about Server Functions for client-server communication
- Explore SSR for server-side rendering
- Review Deployment for production considerations
- Understand Full-Stack Architecture for the complete picture