Base class for response types that stream an iterable payload to the
client memory-efficiently.
Concrete subclasses such as {@see \Cake\Http\Response\JsonStreamResponse} produce the wire format (JSON, CSV, XML, …); this class owns the streaming lifecycle that is common to all of them:
X-Accel-Buffering: no so proxies / nginx do not buffer the response).flushEvery option.cakephp/log being
installed) for surfacing encoding failures server-side.Subclasses must implement {@see self::contentType()} (the MIME type without the charset suffix) and {@see self::streamData()} (the actual write loop). They may override {@see self::normalizeStreamOptions()} to validate format-specific options on top of the base validation.
A response class for streaming large JSON datasets memory-efficiently.
Uses true streaming - data is flushed to the client as each item is encoded, keeping memory usage constant regardless of dataset size. Supports both standard JSON arrays and NDJSON (newline-delimited JSON) formats.
// Simple array streaming
return new JsonStreamResponse($query);
// With root wrapper
return new JsonStreamResponse($query, ['root' => 'articles']);
// NDJSON format
return new JsonStreamResponse($query, ['format' => 'ndjson']);
root (string|null, default: null): Wrap data in {"root": [...]}envelope (array, default: []): Static metadata merged with streaming datadataKey (string, default: 'data'): Key for streaming data when envelope is usedformat (string, default: 'json'): Output format — 'json' or 'ndjson'transform (callable|null, default: null): Transform each item before encodingflags (int, default: DEFAULT_JSON_FLAGS): JSON encode flagsflushEvery (int, default: 1): Flush output buffers every N itemsFor true streaming benefits, use unbuffered queries and avoid result formatters:
// Good - streams one row at a time
$query = $this->Articles->find()->disableBufferedResults();
return new JsonStreamResponse($query);
// Avoid - formatters like map(), combine() buffer results internally
$query = $this->Articles->find()->map(fn($row) => $row); // Breaks streaming
With true streaming, memory usage stays constant:
A response redirecting the client to another location.
The response has an empty body and no Content-Type header.
return new RedirectResponse('/articles', 301, ['X-Reason' => 'moved']);