I'm writing a server sent event stream in PHP and I receive the error:
"failed to flush buffer zlib output compression"
This I believe is due to trying to flush the gzipped output.
Here's my PHP code:
header ("Content-Type: text/event-stream\n\n");
header ("Cache-Control: no-cache");
echo "data: {$json}";
echo "\n\n";
ob_flush(); // ERROR HERE
flush();
My question is what is the best way to get this working - ideally without disabling gzip in apache - can it be turned off in PHP?
I tried this but it didn't work:
if(ini_get('zlib.output_compression')){
ini_set('zlib.output_compression', 'Off');
}
You cannot use zlib output compression along ob_ output handler. See the php docs on zlib.output_compression, it states it multiple times.
If found out that the easiest way to enable output compression in php is to just do this:
ini_set("zlib.output_compression", 1);
ini_set("zlib.output_compression_level", 9);
And loose all the ob_* stuff. Now when a client requests a page with a header like :
Accept-Encoding: gzip, deflate
zlib will gzip your response body for you AND it will set this for you in the response:
Content-Encoding: gzip
I spent hours of my life on this before realising how simple it is with those 2 lines. and don't use any flush at all implicitely