Skip to content

Commit 0f44074

Browse files
Document Response "stream" and "streamJson" methods (#9881)
* Document response "stream" and "streamJson" methods * formatting * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 62eb15f commit 0f44074

File tree

1 file changed

+47
-9
lines changed

1 file changed

+47
-9
lines changed

responses.md

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [JSON Responses](#json-responses)
1515
- [File Downloads](#file-downloads)
1616
- [File Responses](#file-responses)
17+
- [Streamed Responses](#streamed-responses)
1718
- [Response Macros](#response-macros)
1819

1920
<a name="creating-responses"></a>
@@ -287,6 +288,52 @@ The `download` method may be used to generate a response that forces the user's
287288
> [!WARNING]
288289
> Symfony HttpFoundation, which manages file downloads, requires the file being downloaded to have an ASCII filename.
289290
291+
<a name="file-responses"></a>
292+
### File Responses
293+
294+
The `file` method may be used to display a file, such as an image or PDF, directly in the user's browser instead of initiating a download. This method accepts the absolute path to the file as its first argument and an array of headers as its second argument:
295+
296+
return response()->file($pathToFile);
297+
298+
return response()->file($pathToFile, $headers);
299+
300+
<a name="streamed-responses"></a>
301+
### Streamed Responses
302+
303+
By streaming data to the client as it is generated, you can significantly reduce memory usage and improve performance, especially for very large responses. Streamed responses allow the client to begin processing data before the server has finished sending it:
304+
305+
function streamedContent(): Generator {
306+
yield 'Hello, ';
307+
yield 'World!';
308+
}
309+
310+
Route::get('/stream', function () {
311+
return response()->stream(function (): void {
312+
foreach (streamedContent() as $chunk) {
313+
echo $chunk;
314+
ob_flush();
315+
flush();
316+
sleep(2); // Simulate delay between chunks...
317+
}
318+
}, 200, ['X-Accel-Buffering' => 'no']);
319+
});
320+
321+
> [!NOTE]
322+
> Internally, Laravel utilizes PHP's output buffering functionality. As you can see in the example above, you should use the `ob_flush` and `flush` functions to push buffered content to the client.
323+
324+
<a name="streamed-json-responses"></a>
325+
#### Streamed JSON Responses
326+
327+
If you need to stream JSON data incrementally, you may utilize the `streamJson` method. This method is especially useful for large datasets that need to be sent progressively to the browser in a format that can be easily parsed by JavaScript:
328+
329+
use App\Models\User;
330+
331+
Route::get('/users.json', function () {
332+
return response()->streamJson([
333+
'users' => User::cursor(),
334+
]);
335+
});
336+
290337
<a name="streamed-downloads"></a>
291338
#### Streamed Downloads
292339

@@ -300,15 +347,6 @@ Sometimes you may wish to turn the string response of a given operation into a d
300347
->readme('laravel', 'laravel')['contents'];
301348
}, 'laravel-readme.md');
302349

303-
<a name="file-responses"></a>
304-
### File Responses
305-
306-
The `file` method may be used to display a file, such as an image or PDF, directly in the user's browser instead of initiating a download. This method accepts the absolute path to the file as its first argument and an array of headers as its second argument:
307-
308-
return response()->file($pathToFile);
309-
310-
return response()->file($pathToFile, $headers);
311-
312350
<a name="response-macros"></a>
313351
## Response Macros
314352

0 commit comments

Comments
 (0)