content/docs/07-reference/01-ai-sdk-core/75-simulate-readable-stream.mdx
simulateReadableStream()simulateReadableStream is a utility function that creates a ReadableStream which emits provided values sequentially with configurable delays. This is particularly useful for testing streaming functionality or simulating time-delayed data streams.
import { simulateReadableStream } from 'ai';
const stream = simulateReadableStream({
chunks: ['Hello', ' ', 'World'],
initialDelayInMs: 100,
chunkDelayInMs: 50,
});
<Snippet text={import { simulateReadableStream } from "ai"} prompt={false} />
<PropertiesTable content={[ { name: 'chunks', type: 'T[]', isOptional: false, description: 'Array of values to be emitted by the stream', }, { name: 'initialDelayInMs', type: 'number | null', isOptional: true, description: 'Initial delay in milliseconds before emitting the first value. Defaults to 0. Set to null to skip the initial delay entirely.', }, { name: 'chunkDelayInMs', type: 'number | null', isOptional: true, description: 'Delay in milliseconds between emitting each value. Defaults to 0. Set to null to skip delays between chunks.', }, ]} />
Returns a ReadableStream<T> that:
chunks array sequentiallyinitialDelayInMs before emitting the first value (if not null)chunkDelayInMs between emitting subsequent values (if not null)T: The type of values contained in the chunks array and emitted by the streamconst stream = simulateReadableStream({
chunks: ['Hello', ' ', 'World'],
});
const stream = simulateReadableStream({
chunks: ['Hello', ' ', 'World'],
initialDelayInMs: 1000, // Wait 1 second before first chunk
chunkDelayInMs: 500, // Wait 0.5 seconds between chunks
});
const stream = simulateReadableStream({
chunks: ['Hello', ' ', 'World'],
initialDelayInMs: null, // No initial delay
chunkDelayInMs: null, // No delay between chunks
});