HTML and CSS Reference
The event stream itself is similar to the forever-frame approach (see appendix D ). A con-
nection is opened and kept open, and the chat.js script periodically adds content to it.
Each time new content arrives at the browser, it's converted into the simple event-driven
The code on the server is straightforward. Create a file sse.php to generate the event
stream in the same directory as index.php and add the same session_start() , in-
clude_once , and PDO creation code that starts off index.php. You don't need to add a
!DOCTYPE declaration because you're not generating an HTML page. Then add code to
loop, constantly looking for new messages. If you already have a forever-frame script, it's
likely you can easily adapt it. The code for sse.php is shown in the following listing.
Listing 4.8. sse.php key code loop