HTML5 and CSS3, Second Edition

(singke) #1

Tip 32


Tip 32. Chatting with Web Sockets


Web developers have been pursuing real-time interaction with users for many
years, but most of the implementations have involved using JavaScript to
periodically hit the remote server to check for changes. HTTP is a stateless
protocol, so a web browser makes a connection to a server, gets a response,
and disconnects. Doing any kind of real-time work over a stateless protocol
can be quite rough. The HTML5 specification introduced web sockets, which
let the browser make a stateful connection to a remote server.^6 We can use
web sockets to build all kinds of great applications. One of the best ways to
get a feel for how they work is to write a chat client—which, coincidentally,
AwesomeCo wants for its support site.

AwesomeCo wants to create a simple web-based chat interface on its support
site that will let geographically dispersed members of the support staff com-
municate internally. See the following figure.

Figure 34—Our chat interface



  1. Web sockets have been spun off into their own specification, which you can find at
    http://www.w3.org/TR/websockets/.


report erratum • discuss

Chatting with Web Sockets • 219


Download from Wow! eBook <www.wowebook.com>

Free download pdf