title: How to fire EventSource SSE events?
body: <p>I've recently discovered EventSource, YUI3 has a Gallery module to normalise and fallback behaviour, that's what I've chosen to go with in my example as I use that framework already.</p>
  
  <p>So I've searched about quite a bit, read many blogs, posts and examples, all of which show pretty much the same thing: How to set up basic SSE events. I now have 6 examples of open/message/error/close events firing.</p>
  
  <p>What I don't have (what I'd hoped <a href="http://googlecodesamples.com/html5/sse/sse.html" rel="nofollow noreferrer">this link</a> was going to give me) is an example of how to fire SSE events which are more useful to my application, I'm trying one called 'update'.</p>
  
  <p>Here's is my basic test page: <a href="http://codefinger.co.nz/public/yui/eventsource/test.php" rel="nofollow noreferrer">http://codefinger.co.nz/public/yui/eventsource/test.php</a> (it might as well be an html file, there's no php code in here yet)</p>
  
  <p>And here's the 'message.php' in the EventSource constructor:</p>
  
  <pre><code>&lt;?php
  header('Content-Type: text/event-stream');
  header('Cache-Control: no-cache'); // recommended to prevent caching of event data.
  
  /**
   * Constructs the SSE data format and flushes that data to the client.
   *
   * @param string $id Timestamp/id of this connection.
   * @param string $msg Line of text that should be transmitted.
   */
  function sendMsg($id, $msg) {
    echo "id: $id" . PHP_EOL;
    echo "data: $msg" . PHP_EOL;
    echo PHP_EOL;
    ob_flush();
    flush();
  }
  while(true) {
    $serverTime = time();
    sendMsg($serverTime, 'server time: ' . date("h:i:s", time()));
    sleep(10);
  }
  
  // I was hoping calling this file with a param might allow me to fire an event,
  // which it does dutifully, but no browsers register the 'data : update' - though
  // I do see the response in Firebug.
  if( $_REQUEST['cmd'] ){
      sendMsg($serverTime, $_REQUEST['cmd'] );
  }
  ?&gt;
  </code></pre>
  
  <p>From the live example above, you can see that I've tried to use YUI's io module to send a request, with param, to fire my 'update' event when I click the 'update' button. It seems to work, as you can see in Firebug's Net panel, but my event isn't handled (I realise the script above will run that loop again, I just want to get my event handled in connected browsers, then I'll remove/cleanup).</p>
  
  <p>Am I doing this part wrong? Or is there something more fundamental I'm doing wrong? I'm trying to push events in response to my UI's state changing.</p>
  
  <p><a href="https://stackoverflow.com/questions/9070995/html5-server-sent-events-prototyping-ambiguous-error-and-repeated-polling">This SO question</a> seemed to come close, @tomfumb commented that his next question was going to be "how to send new events to the client after the initial connection is made - now I see that the PHP just has to never stop executing." But surely I'd only send events as they happen... and not continuously...</p>