- Beyond Basic HTML
- Bridging the Divide
- Getting Things Done with WebSockets and Web Workers
- Application Cache
- Database API
- Web Storage
- Geolocation
- Getting Users' Attention with Notifications
- Media Elements
- HTML5 Drawing APIs
- Conveying Information with Microdata
- Summary
Getting Things Done with WebSockets and Web Workers
One of the additions to HTML5 is APIs that help the web application communicate and do work. WebSockets allow web applications to open a channel to interact with web services. Web Workers permit them to run nontrivial tasks without locking the browser.
WebSockets
WebSockets allow applications to have a bidirectional channel to a URI endpoint. Sockets can send and receive messages and respond to opening or closing a WebSocket. Although not part of the specification, two-way communication can be achieved in several other ways, including Comet (AJAX with long polling), Bayeux, and BOSH.
Listing 1-1 shows the code to create a WebSocket that talks to the echo server endpoint. After creating the socket, we set up the functions to be executed when the socket is opened, closed, receives a message, or throws an error. Next, a "Hello World!" message is sent, and the browser displays "Hello World!" upon receipt of the return message.
Listing 1-1. WebSocket Code for Echoing a Message
var socket = new WebSocket(ws://websockets.org:8787/echo); socket.onopen = function(evt) { console.log("Socket opened");}; socket.onclose = function(evt) {console.log("Socket closed");}; socket.onmessage = function(evt){console.log(evt.data);}; socket.onerror = function(evt) {console.log("Error: "+evt.data);}; socket.send("Hello World!");
Web Workers
Web Workers are the HTML5 incarnation of WorkerPools in Google Gears. Unlike WorkerPools, we don't have to create a pool to house our Web Workers. Listing 1-2 shows the code to create a simple worker and set a function for it to execute upon receipt of a message. Listings 1-2 and 1-3 show the HTML code for creating a web page with a Web Worker that displays the current date and time on two-second intervals.
Listing 1-2. Web Page for Requesting the Time
<!DOCTYPE HTML> <html> <head> <title>Web Worker example</title> </head> <body> <p>The time is now: <span id="result" /></p> <script> var worker = new Worker('worker.js'); worker.onmessage = function (event) { document.getElementById('result').innerText = event.data; }; </script> </body> </html>
The associated JavaScript worker.js file is shown in Listing 1-3.
Listing 1-3. Worker.js File for Getting a Date and Time
setInterval(function() {w postMessage(new Date()); }, 2000);
In the two listings, we see that workers can send messages using postMessage() and can listen for messages on the closure onmessage. We can also respond to errors and terminate workers by passing a function to onerror and executing terminate(), respectively.
Workers can be shared and send messages on MessagePorts. As with other aspects of the Web Worker spec, this portion is in a state of flux and somewhat outside the needs of the examples in this book. Therefore, using SharedWorkers is left as an exercise for the reader to investigate.