While poking around the Web Worker API in the latest Opera Beta Release, I discovered that they had also implemented support for Shared Web Workers. If you’re not familiar with Shared Web Workers, have a look here. The basic premise is that a Shared Worker can have multiple connections made to one Worker.
Paraphrased from the specification for clarity:
[Instead of a single message processing function, workers can attach multiple event listeners, each one performing a quick check to see if it is relevant for the message. If multiple authors wanted to collaborate using a single port to communicate with a worker, it would allow for independent code instead of changes having to all be made to a single event handling function.]
So how about some code? In order to run the demo I’ve prepared, you’ll need Opera 10.6 Beta 1. Then hop over to Github and grab the Gist.
The comments will guide you through each step of the demo’s functionality.
Basic HTML page for running the test:
The HTML page called in the iframe:
The Renderer (that’s your browser window)
The SharedWorker
This demonstrates how we can connect two different pages to the same SharedWorker process, and track our connections to them from one persistent object variable. Very exciting!
*Edit*
In the time since this was originally published, Chrome,
Safari &
Opera now support complex JSON messages.
2 Comments
For all other browsers (including IE8+), you can simulate shared workers by using the “storage” event and modifying properties on the sessionStorage object, which is shared across all views with the same origin, just like shared workers. However, you’ll need to listen to the event on the document instead of the document view in IE8 due to a bug.
Thanks for the additional information, however it should be noted that a “simulated” worker thread or shared worker thread is never going to provide the same benefit that you with true parallel execution environment.
4 Trackbacks
[...] Additionally, I discovered support for Shared Workers [...]
[...] This post was mentioned on Twitter by Molly E. Holzschlag, Phred, Alistair MacDonald, rick waldron, Eli Grey and others. Eli Grey said: RT @rwaldron: New Blog! Opera 10.6 Beta supports SharedWorkers! Demo gists included: http://gul.ly/7l #javascript #opera #sharedwebworke … [...]
[...] my excitement and haste upon discovering new SharedWorker() support in the Opera 10.60 Beta, I neglected to snoop around the window object of my two favorites, Chrome 5+ and FireFox 3.6+ [...]
[...] Waldron has detailed the SharedWorker support that Opera has added in 10.6 beta (and has been available in Safari 5 and Chrome [...]