HTML and CSS Reference
In-Depth Information
Browsers are effectively single threaded applications, in that
when JavaScript is running or perhaps being parsed, the page
isn't rendering. Equally, when JavaScript is performing a long
and complicated function, the whole browser can be seen to
lock up. What Web Workers does is introduce a simplifi ed idea
of threads for browsers. A worker allows me to ring fence a par-
ticular block of code and it will run without affecting the browser
at all, as if I have created a new thread of operation allowing the
main browser thread to continue uninterrupted.
Finally, sockets are a way of creating a connected stream to
your server (for server boffs out there: a TCP connection), and
allows two-way real-time communication between the server
and the client. The typically hello world app is a chat client, but
the possibilities of use are endless. Sockets go a long way
towards replacing Comet-based code. Comet uses a variety of
techniques to achieve real-time streaming data from a server.
Web Sockets simplify this process on the client side, as we'll
see later in this chapter.
NOTE The new
XMLHttpRequest level 2
object (
XMLHttpRequest2/ ), out of
scope for this topic but already
in WebKit and Firefox, supports
cross-domain requests (with the
right level of server security). It
also includes progress events
for monitoring uploads.
Chit chat with the Messaging API
I wanted to show you the Messaging API fi rst because the next
two APIs, Web Workers and Web Sockets, both use this com-
mon method of communication. So think of this as your gentle
primer on communication.
The Messaging API has very good support across all brows-
ers (yes, including IE) and offers a simple API for posting plain
text messages from one origin (or domain, to you and me) to
another. For example, if you want to send some information to a
widget you have in an iframe, you can do it using the Messaging
API. This will still work if the widget is on a completely different
domain than the site hosting the iframe.
Sending messages across domains
If Bruce has a document that wants to communicate with my
document, say either in an iframe or perhaps in a popup win-
dow, it needs a reference to the window object (of my document)
and he can then call the postMessage method to pass some mes-
sage to it. The JavaScript in Bruce's document will look like this:
Search WWH ::

Custom Search