Baldinof / roadrunner-bundle

A RoadRunner worker integrated in your Symfony app
MIT License
266 stars 47 forks source link

Implement Generator-based response streaming #138

Open ivanpepelko opened 6 months ago

ivanpepelko commented 6 months ago

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.

praswicaksono commented 3 days ago

Hi @ivanpepelko you might want to look this PR: https://github.com/Baldinof/roadrunner-bundle/pull/130/files it seem similar with yours maybe you can take over from there

ivanpepelko commented 3 days ago

To be honest, I'm out of the loop on this issue. In my project, I replaced this library with fluffydiscord/roadrunner-symfony-bundle, which supports response streaming. Also, not having PR merged on symfony side is a deal breaker for this PR.