Unable to flush a response on Symfony server

I can’t seem to flush the write buffer when running Symfony server. It seems to always sleep for the full amount of time first and then display the full output. Is there something I’m missing?

Basic no-frills example

class SSEController extends AbstractController
{
    public function index()
    {
        echo 1;
        ob_flush();
        flush();
        sleep(4);
        echo 2;
        ob_flush();
        flush();
        exit;
    }
}

StreamedResponse example

$response = new StreamedResponse();
$response->headers->set('Content-Type', 'text/event-stream');
$response->headers->set('Cache-Control', 'no-cache');
$response->headers->set('Connection', 'keep-alive');
$response->headers->set('X-Accel-Buffering', 'no');
$response->headers->set('Content-Type', 'text/event-stream');
$response->setCallback(function() {
    echo 1;
    ob_flush();
    flush();
    sleep(4);
    echo 2;
    ob_flush();
    flush();
});

return $response;

For what it’s worth, this is all in attempt to get php-sse working, but I’ve withdrawn from using its functionality for now because it appears that I need to get flush() working first.

  • Symfony server command: symfony server:start
  • Symfony version: 5.0.8
  • PHP version: 7.2.7

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *