Skip to main content

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:
  1. HTML Streaming: Stream rendered HTML from server to browser
  2. Data Streaming: Stream data from server functions to client

HTML Streaming

Stream Handler

Use defaultStreamHandler for HTML streaming:
The stream handler:
  1. Begins sending HTML immediately
  2. Streams content as components render
  3. Handles deferred data automatically
  4. Injects hydration data
Reference: packages/start-server-core/src/createStartHandler.ts:353-359

Deferred Data with Streaming

Combine deferred data with streaming for optimal performance:
Reference: examples/react/start-basic/src/routes/deferred.tsx:18-62

How HTML Streaming Works

Data Streaming

ReadableStream

Return a ReadableStream from server functions:
Reference: examples/react/start-streaming-data-from-server-functions/src/routes/index.tsx:58-74

Async Generators

Use async generators for cleaner streaming code:
Reference: 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:
Reference: packages/start-server-core/src/server-functions-handler.ts:301-308

Framed Protocol

For complex data with raw streams:
This protocol supports:
  • JSON-serializable data
  • Binary streams (files, images)
  • Multiple concurrent streams
Reference: 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:
Reference: 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