roadrunner-bundle icon indicating copy to clipboard operation
roadrunner-bundle copied to clipboard

Implement Generator-based response streaming

Open ivanpepelko opened this issue 11 months ago • 2 comments

POC of proper Response streaming.

I'm currently working on an app which uses Response streaming. It's a simple use case, where progress updates are pushed to the client. The team uses both symfony-cli runtime and Roadrunner.

With this PR, I'd like to open a discussion on whether this is the correct approach (eg. I'm not sure this extended version of StreamedResponse fits into this project).

IMPORTANT: This will not work without appropriate change in symfony/http-kernel, which wraps Response callback and does not return the value (which must be a Generator instance) (PR).

This update changes the client API. Before:

use Symfony\Component\HttpFoundation\StreamedResponse;

$response = new StreamedResponse();
$response->setCallback(function (): void {
    echo 'Hello World';
    flush();
    sleep(2);
    echo 'Hello World';
    flush();
});

After:

use Baldinof\RoadRunnerBundle\Http\StreamedResponse;

$response = new StreamedResponse();
$response->setCallback(function (): void {
    yield 'Hello World';
    sleep(2);
    yield 'Hello World';
});


At the moment, in my project I've added composer hook to patch files from the PR. This is not an optimal solution as it adds unnecessary maintenance overhead.

ivanpepelko avatar Mar 14 '24 11:03 ivanpepelko