Chapter 4. Messaging: communicating to and from scripts in HTML5

 

Chapter 4 at a glance

Topic

Description, methods, and so on

Page

Server-sent events Creating events in the browser from the server:  
 
  • Creating an EventSource()
111
 
  • Listening to server events with addEventListener()
111
WebSockets Two-way, event-driven communication:  
 
  • Writing applications using WebSockets
116
 
  • Messaging on the client side
125
Cross-document messaging Communication between scripts in different windows:  
 
  • Sending messages with postMessage()
126
 
  • Receiving messages with onmessage()
126

Look for this icon throughout the chapter to quickly locate the topics outlined in this table.

This chapter covers

  • Server-sent events and event-driven communications from the server
  • WebSockets for bidirectional, event-driven communication
  • Client-side messaging between pages from different domains

In the last decade, the web has moved from communication based on uploading static content, similar to the traditional print publishing model, to a real-time communication system where tweets and friendings are instantly announced to hundreds of followers. We’ve all become so used to dynamically updating web pages that we don’t realize most of this is built as a series of hacks on top of HTML4 and HTTP 1.0/1.1. HTML5 cleans up these hacks by providing well-defined APIs for messaging—between the browser and web servers and between different iframes or other objects loaded in the browser.

4.1. Server-sent events (SSE)

4.2. Using WebSockets to build a real-time messaging web app

4.3. Messaging on the client side

4.4. Summary