0
votes

When using php-mod and fastcgi the code executes perfectly and every second i get an output but switching to php-fpm the code lags a few seconds before outputting depending on output size

Tried the following and combinations of setting output buffer 0 in php ini ob_implicit_flush ob_start ob_end_flush header Content-Encoding = none implicit_flush 1 ob_end_clean

<?php
  header('Content-Type: text/event-stream');
  header('Cache-Control: no-cache');
  while( true ){
    $time = date('r');
    echo "retry:1000\r\n";
    echo "data: ".$time;
    echo "\r\n\r\n";                    
    ob_flush();
    flush();
    sleep(1);
   }
  ?>

This is for a production server and php-mod is not an option i also got it to work in Fastcgi with FcgidOutputBufferSize 0 is there a way to make the code work on php-fpm so the output is send immediately as in php-mod and fastcgi ?

P.S Running : Ubuntu 18.04, Apache 2.4.29, PHP 7.2

1

1 Answers

0
votes

After a few days i have discovered the only way to get this to work in php-fpm is to fill the output buffer. This is really inefficient ! Let me explain :

Say you are using Server-send events and your output buffer is 4096, you process every second even if you do not return anything you still send about 4Kb output to client where mod_php and fast-cgi sends only data when there is an output.

If anyone else has this problem this is my best solution : run main site on php-fpm ex. example.com and make a sub-domain ex. push.example.com and setup fast-cgi / php_mod[NOT RECOMMENDED PRODUCTION] on sub-domain now you can keep the connection open and process data without sending output to client.

PS. I saved Session variables in database so both domain and sub-domain can access it see https://github.com/dominicklee/PHP-MySQL-Sessions the other thing is to let sub-domain send CORS. in PHP add header('Access-Control-Allow-Origin: https://example.com');