O'Reilly logo

Data Push Apps with HTML5 SSE by Darren Cook

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required