
Sometimes you need to send data to users without making them wait a long time. Laravel’s new streamed response functions make this simple! They let you send stuff bit by bit, like watching a video while it downloads. Let’s check out what’s new: stream(), streamJson(), and eventStream().
Usually, when your app sends data, it waits until everything’s ready. That can take forever if the data’s big. Streamed responses fix that by sending small pieces as soon as they’re good to go. It’s faster, uses less memory, and feels smooth for users.
stream(): Sending Anything in ChunksThe stream() function is for sending all kinds of data little by little. It’s great for big files or stuff that takes time to make.
You write a function that sends data in parts. Laravel pushes each part out right away if you use ob_flush() and flush().
Here’s a route that sends “Hello, ” then waits a bit, and sends “World!”:
Route::get('/stream', function () {
return response()->stream(function () {
echo 'Hello, ';
ob_flush();
flush();
sleep(2); // Fake some work
echo 'World!';
ob_flush();
flush();
}, 200, ['X-Accel-Buffering' => 'no']);
});Hit that route, and you’ll see “Hello, ” first, then “World!” after 2 seconds. It’s a chill way to send stuff without loading everything at once.
Note: Make sure
output_bufferingis enabled in your PHP settings (e.g.,output_buffering = 4096inphp.ini), or you might run into the error:ob_flush(): Failed to flush buffer. No buffer to flush.
streamJson(): JSON Without the SlowdownIf your app sends a lot of JSON—like an API with tons of data—streamJson() is perfect. It sends JSON in pieces so the client gets it fast.
Give it an array or a lazy collection (like cursor() for database stuff), and it streams the JSON without choking your server.
Here’s how to stream a list of users:
use App\Models\User;
Route::get('/users.json', function () {
return response()->streamJson([
'users' => User::cursor(), // Grabs users one at a time
]);
});This keeps things quick even with thousands of users.
eventStream(): Real-Time UpdatesWant to send live updates, like a chat or notifications? eventStream() uses Server-Sent Events (SSE) to push stuff to the client right when it happens.
You send StreamedEvent objects with event names and data. The client listens with JavaScript, and Laravel closes the stream when you’re done.
Here’s a chat stream:
use Illuminate\Http\StreamedEvent;
Route::get('/chat', function () {
return response()->eventStream(function () {
$stream = OpenAI::client()->chat()->createStreamed(...);
foreach ($stream as $response) {
yield new StreamedEvent(
event: 'message',
data: json_encode($response->choices[0])
);
}
});
});On the frontend, you’d grab it like this:
const source = new EventSource('/chat');
source.addEventListener('message', (event) => {
console.log('New message:', event.data);
});This keeps users updated without them refreshing.
These streamed responses make your app better:
eventStream().It’s all about keeping things smooth and quick.
Laravel’s streamed responses—stream(), streamJson(), and eventStream()—are easy tools to send data smarter. Use stream() for anything chunky, streamJson() for big JSON, and eventStream() for live updates. Pick what fits your project and watch your app speed up!