title: SSE not sending events until the script has finished executing?
body: <p>I am having a issue where SSE events wait until the script has finished executing before finally sending the data, after hours of searching for a plausible explanation or solution, I have found nothing.</p>
  
  <p>I have found a example however that works on that website, however taking that code onto my server, it doesn't work. (URL: <a href="http://www.binarytides.com/monitor-progress-long-running-php-scripts-html5-server-sent-events/" rel="nofollow">http://www.binarytides.com/monitor-progress-long-running-php-scripts-html5-server-sent-events/</a>)</p>
  
  <p>In this example I have noticed a header of <code>Transfer-Encoding: chunked</code> When I had tried to make Apache send this header through .htaccess with <code>Header set</code>, the response code turned to "(failed)".</p>
  
  <p>I also noticed the example didn't have a <code>Content-Length</code>, where as the one from my server does.</p>
  
  <p>This was all tested on Chrome v.53.0.2785.116m</p>
  
  <p>After that I decided to take it to the stacks.</p>
  
  <p>Here is a example:</p>
  
  <pre><code>&lt;?php
      header('Content-Type: text/event-stream');
      header('Cache-Control: no-cache');
  
      function sendMsg($id, $msg) {
          echo "id: $id" . PHP_EOL;
          echo "data: $msg" . PHP_EOL;
          echo PHP_EOL;
          ob_flush();
          flush();
      }
  
      while ($x &lt; 5) {
          $serverTime = time();
          sendMsg($serverTime, 'server time: ' . date("h:i:s", time()));
          sleep(1);
          $x++;
      }
  ?&gt;
  </code></pre>
  
  <p>If I were to perform a request to this script, I would get these headers:</p>
  
  <pre>
  HTTP/1.1 200 OK
  Date: Tue, 27 Sep 2016 22:34:11 GMT
  Server: Apache
  Cache-Control: no-cache
  Vary: Accept-Encoding,User-Agent
  Content-Encoding: gzip
  Content-Length: 91
  Keep-Alive: timeout=15, max=43
  Connection: Keep-Alive
  Content-Type: text/event-stream
  </pre>
  
  <p>I will not get any data from this until the script had finished executing.</p>
  
  <p>At this point I'm unclear if its Apache, or PHP that is at fault here, but from what I can tell the server is at fault.
  I've tried version of PHP ranging from 5.5-7, all with no luck.</p>
  
  <p>Any ideas on whats going wrong here and how to fix it?</p>
  
  <p><strong>EDIT 1:</strong>
  I tried the same script on another server and it had worked properly, and sent the following headers:</p>
  
  <pre>
  HTTP/1.1 200 OK
  Server: nginx
  Date: Wed, 28 Sep 2016 15:40:00 GMT
  Content-Type: text/event-stream;charset=UTF-8
  Transfer-Encoding: chunked
  Connection: keep-alive
  Keep-Alive: timeout=15
  Cache-Control: no-cache
  ngpass_ngall: 1
  </pre>