Server-Sent Events

Web sockets are cool, but they require a different protocol and are really meant for two-way communication. If you just need to push data to the client from the server, you can use server-sent events, or SSE, which works over regular old HTTP.

To demonstrate this we’ll set up a very basic page that will display messages from our server. The web server available in the book’s example code already has support for SSE, so we’ll focus on the client-side implementation. Create a simple HTML page like this, with a place for the messages to display:

where_next/html5_sse/index.html
 
<!DOCTYPE html>
 
<html>
 
<head>
 
<meta​ charset=​"utf-8"​​>
 
<title>​AwesomeCo Messages​</title>
 
<link​ rel=​"stylesheet"​ href=​"stylesheets/style.css" ...

Get HTML5 and CSS3, 2nd Edition 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.