title: HTML5 Server-Sent Events via cURL
body: <p>I'm trying to make an HTML5-based notification system, based on when something occurs on a table in my database. I have specifically made the table for this notification feature . This is how it works:</p>
  
  <ol>
  <li>Server-Sent Events page (<code>sse.html</code>) will refer to <code>source.php</code> for its notification content.</li>
  <li><code>action.php</code> contains the action that would trigger  a change of content notification by inserting data in to a table.</li>
  <li><code>source.php</code> will periodically check for any new data in table. It should pass a notification if there's any.</li>
  </ol>
  
  <p>My question is it possible if to do this without checking the table?</p>
  
  <p>I'm thinking of using cURL to directly send content to <code>source.php</code> when the action in <code>action.php</code> is performed. Can this be done?</p>
  
  <p>I've seen cURL used with the HTTP header <code>Content-Type</code> set to <code>text/event-stream</code>, but I'm not sure how to use it.</p>
  
  <p><strong><code>sse.html</code></strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;  
  &lt;html&gt;  
  &lt;body&gt;  
    &lt;script&gt;  
      var source = new EventSource('source.php');  
      source.onmessage = function(event){  
        var text = event.data;  
        window.webkitNotifications.createNotification('', 'Alert', text).show();  
      }  
    &lt;/script&gt;  
  &lt;/body&gt;  
  &lt;/html&gt;
  </code></pre>
  
  <p><strong><code>source.php</code></strong></p>
  
  <pre><code>header("Content-Type: text/event-stream");
  header("Cache-Control: no-cache");
  
  mysql_connect("localhost", "user", "pass");
  mysql_select_db("eventstream");
  
  $q = mysql_query("select textnotif from notification where read='0'");
  $r = mysql_fetch_array($q);
  
  $notif = $r[textnotif];
  
  if($notif != ""){  
      echo "data: ".$notif.PHP_EOL;  
  }
  </code></pre>