The $response->stream($fn) primitive gives you fine-grained control: $fn receives a $write(string) closure that flushes chunks the moment you call it. Useful when you want to decide what to send at run time instead of yielding a fixed sequence.
$app->route('/stream/words', function ($response) {
return $response->stream(function ($write) {
foreach (explode(' ', 'streamed word by word from the server') as $w) {
$write($w . ' ');
co::sleep(0.15); // a real handler waits on real I/O instead
}
});
});