title: SSE incredibly slow
body: <p>I am currently writing the communication framework for a web game, the communications map can be seen below: <a href="https://i.stack.imgur.com/jsEnu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jsEnu.png" alt=""></a> The code is as follows:</p>
  
  <p>test.php:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt; Test &lt;/title&gt;
          &lt;script&gt;
              function init()
              {
                  var source = new EventSource("massrelay.php");
                  source.onmessage = function(event)
                  {
                      console.log("massrelay sent: " + event.data);
                      var p = document.createElement("p");
                      var t = document.createTextNode(event.data);
                      p.appendChild(t);
                      document.getElementById("rec").appendChild(p);
                  };
              }
  
              function test()
              {
                  var xhr = new XMLHttpRequest();
                  xhr.onreadystatechange = function () 
                  {
                      if(xhr.readyState === XMLHttpRequest.DONE &amp;&amp; xhr.status === 200) 
                      {
                          console.log("reciver responded: " + xhr.responseText);
                      }
                  }
                  xhr.open("GET", "reciver.php?d=" + document.getElementById("inp").value , true);
                  xhr.send();
                  console.log("you sent: " + document.getElementById("inp").value);
              }
          &lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;button onclick="init()"&gt;Start Test&lt;/button&gt; 
          &lt;textarea id="inp"&gt;&lt;/textarea&gt;
          &lt;button onclick="test()"&gt;click me&lt;/button&gt;
          &lt;div id="rec"&gt;&lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This takes user input (currently a textbox for testing) and sends it to the receiver, and writes back what the receivers response to the console, i have never received an error from the receiver.  it also  adds an event listener for the SSE that is sent. </p>
  
  <p>reciver.php:</p>
  
  <pre><code>&lt;?php 
      $data = $_REQUEST["d"];
      (file_put_contents("data.txt", $data)) ? echo $data : echo "error writing";
  ?&gt;
  </code></pre>
  
  <p>This as you can see is very simple and only functions to write the data to data.txt before sending back that the write was successful. data.txt is simply the "tube" data is passed through to massrelay.php.</p>
  
  <p>massrelay.php:</p>
  
  <pre><code>&lt;?php
      header('Content-Type: text/event-stream');
      header('Cache-Control: no-cache');
      while(1)
      {
          $data = file_get_contents("data.txt");
          if ($data != "NULL")
          {
              echo "data: " . $data . "\n\n";
              flush();
              file_put_contents("data.txt", "NULL");
          }
      }
  ?&gt;
  </code></pre>
  
  <p>massrelay.php checks if there is any data in data.txt and if so will pass it using SSE to anyone with an event listener for it, once it reads the data it will clear the data file.</p>
  
  <p>The entire thing actually works perfectly except for the slight ishue that it can take anywhere from 30 seconds to 10 minutes for massrelay.php to send the data from the data file. For a web game this is completely unacceptable as you need real time action. I was wondering if it was taking so long due to a flaw in my code or if not im thinking hardware (Hosting it myself on a 2006 Dell with a sempron). If anyone sees anything wrong with it please let me know thanks.</p>