Skip to content

Conversation

@florianraith
Copy link
Contributor

@florianraith florianraith commented Nov 2, 2025

Introduce $response->stream() and $response->streamLines() methods for the Http Client to consume streamed responses without manual getBody()/eof()/read() loops. Both methods return generators, mirroring how you produce streamed responses with response()->stream(/* ... */). Improves ergonomics and supports line-based protocols (e.g., SSE/NDJSON).

// Before
$stream = Http::withOptions([ 'stream' => true ])
    ->get("http://localhost/stream")
    ->getBody();

// You have to handle line separation on your own
while (! $stream->eof()) {
    $chunk = $stream->read(1);
    echo $chunk;
}

// After
$stream = Http::withOptions([ 'stream' => true ])
    ->get("http://localhost/stream")
    ->stream(); // or ->streamLines() for getting lines as chunks

foreach ($stream as $chunk) {
    echo $chunk;
}

@rodrigopedra
Copy link
Contributor

Is the chunk size in bytes?

If so, wouldn't it be better to use a larger chunk size by default?

florianraith and others added 2 commits November 2, 2025 15:46
Co-authored-by: Luke Kuzmish <[email protected]>
@florianraith
Copy link
Contributor Author

Is the chunk size in bytes?

If so, wouldn't it be better to use a larger chunk size by default?

I initially chose a chunk size of 1024, then changed it to 1 to keep the default less arbitrary. I agree a larger default is probably more appropriate for performance, but I'm unsure what the best value should be. Happy to adjust based on maintainer guidance.

@vadimonus
Copy link
Contributor

Your method names are confusting. When naming something as stream() many users will expect it returns stream resource or StreamInterface.

In your case you're returning generator or iterator, and using result in foreach ($stream as $chunk), it's better to call methods with generator/iterator/chunk in name.

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants