title: server sent events unexpected stop
body: <p>let me first say that I'm no expert in web development so I might have made a stupid mistake but no amount of googling seems to be helping. I need to have a single html page (preprocessed in php) to display occasional events fired by a &quot;server&quot; page residing on the same machine.
  I've readapted the basic w3schools server sent events example as follows and it seemed to be working fine until last night, but today (still working when first tested) I added a simple table and a ref to an external (empty) js file to the html and events stopped being caught by the page. I decided to roll back to the working code but even that doesn't work anymore. I had to remove the files and restore from a backup!
  I'm sure the sse.php code is being run since part of what it does is removing records from a sqlite3 database and that's happening.
  Here's the code, I really hope you can help me because I really have no idea what's happening.
  This was tested on Linux + Xampp + Firefox and sadly this is a mandatory combination, having it work under other conditions is not useful at the moment. (fyi: opening the mon.php page in Opera gave me a single event and then stopped working as well)
  Thank you all.</p>
  <p><strong>mon.php</strong></p>
  <pre><code>&lt;?php
      if ( ! isset($_GET['mon']) ) {
          die('mon code missing');
      }
  ?&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;script type=&quot;text/javascript&quot;&gt;
          var evtSourceUrl = &quot;sse.php?mon=&quot; + &lt;?php echo '&quot;'.$_GET['mon'].'&quot;'?&gt;;
          if(typeof(EventSource) !== &quot;undefined&quot;) {
              var source = new EventSource(evtSourceUrl);
              source.onmessage = function(event) {
                  document.getElementById(&quot;info&quot;).innerHTML += event.data + &quot;&lt;br&gt;&quot;;
              };
          } else {
              document.getElementById(&quot;info&quot;).innerHTML = 'sse not supported';
          }
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;p&gt;
          &lt;span id=&quot;info&quot;&gt;&lt;/span&gt;
      &lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p><strong>sse.php</strong></p>
  <pre><code>&lt;?php
      function output_sse($msg) {
          echo &quot;data: &quot; . $msg . &quot;\n\n&quot;;
          ob_end_flush();
          flush();
      }
  
      header('Content-Type: text/event-stream');
      header('Cache-Control: no-cache');
      header('Connection: keep-alive');
  
      $mon = null;
      if ( isset($_GET[&quot;mon&quot;]) ) {
          $mon = $_GET[&quot;mon&quot;];
      } else {
          output_sse('mon code missing');
          die();
      }
  
      while ( true ) {
          usleep(1000 * 100);
          $db = new PDO(&quot;sqlite:ch.sqlite3&quot;);
          if ( $db === false ) {
            output_sse('err:PDO');
            die();
          }
          $stmt_select = $db-&gt;query(&quot;SELECT * FROM CH WHERE IDMON='$mon';&quot;);
          $stmt_delete = $db-&gt;prepare(&quot;DELETE FROM CH WHERE IDMON='$mon';&quot;);
          $db-&gt;beginTransaction();
          $res = $stmt_select-&gt;fetch();
          $stmt_delete-&gt;execute();
          $db-&gt;commit();
          $db-&gt;close();
          if ( strlen($res['TK']) &gt; 0 &amp;&amp; strlen($res['SP']) &gt; 0 ) {
            $msg = &quot;$res['TK'] :: $res['SP']&quot;;
            output_sse($msg);
          }
      }
  ?&gt;
  </code></pre>
  <p><strong>add.php</strong> (to add new records to be displayed - this works)</p>
  <pre><code>&lt;?php
      $mon = isset($_GET[&quot;mon&quot;]) ? $_GET[&quot;mon&quot;] : die ('mon code missing') ;
      $tk = isset($_GET[&quot;tk&quot;]) ? $_GET[&quot;tk&quot;] : die ('tk code missing') ;
      $sp = isset($_GET[&quot;sp&quot;]) ? $_GET[&quot;sp&quot;] : die ('sp code missing') ;
  
      $db = new SQLite3('ch.sqlite3');
  
      if ( $db === false ) {
          die('Cannot open db');
      }
  
      $res = $db-&gt;exec(&quot;INSERT INTO CH('IDMON','TK','SP') VALUES('$mon','$tk','$sp');&quot;);
      if ( $res != 1 ) {
          echo 'insert failed';
      }
  
      if ( $db-&gt;close() === false ) {
          die('Cannot close connection to db');
      }
      echo('ok');
  ?&gt;
  </code></pre>