1

As in the title described i want to send some data in runtime from Symfony 6 with StreamedResponse() class.

The docs example looks like this:

use Symfony\Component\HttpFoundation\StreamedResponse;

$response = new StreamedResponse();
$response->setCallback(function () {
    var_dump('Hello World');
    //ob_flush();
    flush();
    sleep(2);
    var_dump('Hello World');
    //ob_flush();
    flush();
});
$response->send();

The above example gives the complete output at the end of the script. Some other ideas were to put ob_flush() before flush() but this doesnt change anything. in my php.ini was output_buffering = 4096 Is there another place to change something to send messages at runtime?

Edit (1)

Here is the additional try from a comment with the same result:
    @ob_end_clean();     
    ini_set('output_buffering', 0);          

    $response = new StreamedResponse();
    $response->headers->set('X-Accel-Buffering', 'no');
    $response->headers->set('Content-Type: ', 'text/event-stream');
    $response->headers->set('Cache-Control', 'no-cache, must-revalidate');
    $response->setCallback(function () {
        echo 'Hello World';
        //ob_flush();
        flush();
        sleep(3);
        var_dump('Hello World');
        //ob_flush();
        flush();
    });
    $response->send();

return $response; 

The response headers are:

Cache-Control: must-revalidate, no-cache, private
Content-Type:: text/event-stream
Date: Wed, 31 May 2023 13:54:51 GMT
Set-Cookie: PHPSESSID=h26uhmnc7nq6sibu6ed566k9oe; path=/; secure; HttpOnly; SameSite=lax
X-Accel-Buffering: no
X-Powered-By:PHP/8.1.6
Daniel Richter
  • 768
  • 5
  • 23
  • And how you are reading this output? Maybe your tool only shows output when request has finished? – Justinas May 30 '23 at 08:37
  • E.g.: https://stackoverflow.com/questions/7740646/jquery-read-ajax-stream-incrementally – Justinas May 30 '23 at 08:38
  • to answer both of you: i try ro fetch this with postman and with ajax from symfony and standalone php. I get only the complete output (the example of your link gives me `Begin... (counting to 10)12345678910End...` in 1 Response – Daniel Richter May 30 '23 at 10:15

1 Answers1

0

Try adding this:

@ob_end_clean();
ini_set('output_buffering', 0);
$response->headers->set('X-Accel-Buffering', 'no');

Explain

output_buffering in php.ini will buffer streaming output 100%. To disable it dynamically for individual endpoints, it is not enough to call ini_set, you also need to reset the accumulated buffer via @ob_end_clean (@ - to avoid problems on those environments where the buffer is disabled)

Also often buffering occurs in nginx or something like that. The most convenient way to reset it is through the X-Accel-Buffering header.

See also https://stackoverflow.com/a/76084561/21333278, although it's about Yii, but the proposed solution is universal

vodevel
  • 144
  • 4
  • Its the same behavior. The whole response come at once when the script is finishing – Daniel Richter May 31 '23 at 11:32
  • I checked this code on my Symfony 6, everything works with it (although without it it also sends whole at once). Have you tried the code from the suggested link with setting headers via native php functions like: ```php @ob_end_clean(); ini_set('output_buffering', 0); header('Content-Type: text/event-stream'); header("Cache-Control: no-cache, must-revalidate"); header("X-Accel-Buffering: no"); ``` Also what do the response headers contain? – vodevel May 31 '23 at 12:59
  • i added it in the question above. Can you show me your controller function? – Daniel Richter May 31 '23 at 13:57
  • 1
    There is a typo in your example. Try to remove ":" (colon) sign from 'Content-Type: ' to 'Content-Type'. The rest of the code is working, my controller: ``` #[Route('test-stream', methods: ['GET'])] public function testStream() { # your code }``` – vodevel May 31 '23 at 14:47
  • ok, thats exactly what i have done. Then i have no idea whats wrong. – Daniel Richter May 31 '23 at 16:57
  • @DanielRichter Did you find a solution for that ? I have the same problem (Symfony 6.3) – Sylvain Jun 20 '23 at 15:00
  • Not at the Moment, but anywhere i have read that it doesnt work with the symfony dev Server, but in my case it Changed nothing beteuern public and dev. – Daniel Richter Jun 21 '23 at 16:04