Import
Functions
stream()
Creates a streaming response with full control over the stream.Context
required
The Hono context object
(stream: StreamingApi) => Promise<void>
required
Callback function that receives a StreamingApi instance to write data to the stream
(e: Error, stream: StreamingApi) => Promise<void>
Optional error handler called when an error occurs during streaming
Response
A Response object with a readable stream
streamSSE()
Creates a Server-Sent Events (SSE) streaming response.Context
required
The Hono context object
(stream: SSEStreamingApi) => Promise<void>
required
Callback function that receives an SSEStreamingApi instance
(e: Error, stream: SSEStreamingApi) => Promise<void>
Optional error handler
Response
A Response object with SSE headers and streaming body
streamText()
Creates a text streaming response with appropriate headers.Context
required
The Hono context object
(stream: StreamingApi) => Promise<void>
required
Callback function that receives a StreamingApi instance
(e: Error, stream: StreamingApi) => Promise<void>
Optional error handler
Response
A Response object with text/plain content type and streaming body
Classes
StreamingApi
API for controlling a stream. Methods:write(input: string | Uint8Array): Promise<void>- Write data to streamwriteln(input: string): Promise<void>- Write data with newlinesleep(ms: number): Promise<void>- Sleep for specified millisecondsclose(): Promise<void>- Close the streamabort(): void- Abort the stream
closed: boolean- Whether the stream is closed
SSEStreamingApi
Extends StreamingApi with SSE-specific methods. Methods:writeSSE(message: SSEMessage): Promise<void>- Write an SSE message- All methods from
StreamingApi
Types
SSEMessage
string | Promise<string>
required
The message data. Can be a string or Promise that resolves to a string.
string
Optional event type. Cannot contain
\r or \n.string
Optional message ID. Cannot contain
\r or \n.number
Optional reconnection time in milliseconds
Examples
Real-time Updates
Progress Streaming
AI Streaming Response
SSE with Error Recovery
Headers
streamSSE() automatically sets:
Content-Type: text/event-streamCache-Control: no-cacheConnection: keep-aliveTransfer-Encoding: chunked
streamText() automatically sets:
Content-Type: text/plainX-Content-Type-Options: nosniffTransfer-Encoding: chunked