Chapter 8. More SSE: The Rest of the Standard

The SSE standard contains a few other features that I have glossed over in this book, and this chapter will take a look at them. These other features of SSE have been ignored for a couple of reasons. First, I didn’t need them! By making the decision to always pass around one JSON object per line, and having the JSON object be self-descriptive, the event and multiline features were never needed. The second reason is that it would have made the fallbacks slower and more complicated. By being pragmatic and not trying to create a perfect polyfill of SSE, we could allow our fallbacks to use the protocol that best suited them. But it is good to know about them, and this chapter will introduce each feature, show when you might want to use it, and even give some hints as to how to implement it in our fallbacks.

Headers

Here is a simple script (found as log_headers.html in the book’s source code):

<html>
  <head>
    <title>Logging test</title>
  </head>
  <body>
    <script>
    var es = new EventSource("log_headers.php");
    </script>
  </body>
</html>

This goes to show just how small an SSE script can be. Of course, it does absolutely nothing on the frontend. Here is the corresponding backend script:

<?php
$SSE = (@$_SERVER["HTTP_ACCEPT"] == "text/event-stream");
if($SSE)
  header("Content-Type: text/event-stream");
else
  header("Content-Type: text/plain");
file_put_contents("tmp.log", print_r($_SERVER, true) );
?>

This is also embarrassingly short. It simply writes everything ...

Get Data Push Apps with HTML5 SSE now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.