Skip to main content
The Streaming Helper provides utilities for creating streaming responses, including Server-Sent Events (SSE) and text streaming.

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
Example

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
Example

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
Example

Classes

StreamingApi

API for controlling a stream. Methods:
  • write(input: string | Uint8Array): Promise<void> - Write data to stream
  • writeln(input: string): Promise<void> - Write data with newline
  • sleep(ms: number): Promise<void> - Sleep for specified milliseconds
  • close(): Promise<void> - Close the stream
  • abort(): void - Abort the stream
Properties:
  • 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-stream
  • Cache-Control: no-cache
  • Connection: keep-alive
  • Transfer-Encoding: chunked

streamText() automatically sets:

  • Content-Type: text/plain
  • X-Content-Type-Options: nosniff
  • Transfer-Encoding: chunked