At the Forge - Communication in HTML5
Finally, another intriguing addition to HTML5 is the notion of Web workers, which you can think of as the browser equivalent of threads. Perhaps you have a complex task that needs to be handled in parallel with rendering of a page or of downloading information from the Internet. By splitting the work across two Web workers, you can take advantage of today's modern, multiprocessor computers to get a faster result. Because Web workers operate in the background, rather than on the thread that handles displaying the page, the page should be more responsive than if everything were in one thread.
The idea is this. You launch a Web worker by creating a new Worker object:
var worker = new Worker("code.js");
Notice that you hand the name of a file to a Web worker. You cannot hand it a piece of code, either directly or by passing it a function reference. Perhaps it eventually will be possible for a browser-based application to create dynamically, and then store (using WebSockets?) a file on the server, but the main purpose of this restriction is to ensure that there is no chance for shared data among the various Web workers, thus avoiding the chance for issues traditionally associated with threads.
Indeed, Web workers operate almost as if they existed on different computers, with no direct connections between them. Workers cannot access the DOM, which means any elements on the page. Functions and data in the main thread are not available to the Web workers, and vice versa.
This raises the question of how the main thread and Web workers can communicate. The answer probably won't surprise you. They use postMessage(), the same mechanism for message passing that can be used to send information from one window or tab to another, regardless of origin.
Marc Andreessen, who wrote the original Mosaic browser and helped to popularize the Web as a founder of Netscape, claimed years ago that the browser is the new operating system. Even as Ajax and other advanced Web technologies have advanced during the past few years, and such amazing browser-based applications as Google Docs have emerged, I still have been skeptical of whether Web-based applications ever will truly rival their desktop counterparts. The addition of cross-window communication, WebSockets and Web workers go a long way toward convincing me that Andreessen's prediction has nearly come true.
HTML5 and its associated technologies include a wealth of new options for developers. It will take some time to figure out how well these work, how to get around the fact that not all browsers support them and just how useful (or not) various features might be. If you are a Web developer, I encourage you to study and work with these technologies as soon as possible. I already have changed the architecture of some of my applications as a result, and I wouldn't be surprised if that happens to you too.
The best book I've read on the subject, Dive Into HTML5, isn't even a proper book at the time of this writing, but rather a free on-line resource written by Mark Pilgrim (diveintohtml5.org). If you are familiar with Pilgrim's previous work, such as Dive Into Python, you know his writing is excellent. Not surprisingly, this was the first resource to which I turned to bone up on HTML5, and it continues to be my favorite combination of tutorial and reference.
However, Pilgrim's book says very little on the subjects I've mentioned in this month's column. For excellent tutorials on these subjects, I recommend Pro HTML5 Programming written by Peter Lubbers, Brian Albers and Frank Salim. This last book also is aimed at beginners. Although I think other books are better than this one in other areas, it really shines in the cases I mentioned this month.
Finally, take a look at www.html5rocks.com, a Google-sponsored site that describes various HTML5-related technologies, including documentation and code examples.
Reuven M. Lerner is a longtime Web developer, architect and trainer. He is a PhD candidate in learning sciences at Northwestern University, researching the design and analysis of collaborative on-line communities. Reuven lives with his wife and three children in Modi'in, Israel.
Web Development News
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Numerical Python
- Use Linux as a SAN Provider
- diff -u: What's New in Kernel Development
- NSA: Linux Journal is an "extremist forum" and its readers get flagged for extra surveillance
- RSS Feeds
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- Tech Tip: Really Simple HTTP Server with Python