From what I understand, the big benefit of HTML5's shared web workers is that they can accept multiple connections in a single separate thread of execution.
My question is: has anyone gotten multiple connections with a SharedWorker to work as a single thread with Google Chrome? I'm using latest version 12.0.742.112.
Demo: <a href="http://demos.zulius.com/html5/sharedworker" rel="nofollow">http://demos.zulius.com/html5/sharedworker</a>
Source (in case demo is down): <a href="http://pastebin.com/N5uAsLVR" rel="nofollow">index.html</a>, <a href="http://pastebin.com/DfEux7aP" rel="nofollow">sharedworker.js</a>
The demo establishes 2 separate event listeners. The expected output is:
foo got message: Hello World! You are connection #1 bar got message: Hello World! You are connection #2
In the demo, both event listeners fire correctly, but the connection count variable is not maintained in the SharedWorker script. This leads me to believe each connection to the SharedWorker is executing in a separate thread.
Am I doing something wrong? Or is Chrome support for SharedWorker not quite there?
<strong>UPDATE:</strong> the demo works now.Answer1:
You have 2 listeners to the Worker but you only start the Worker for once, so it's 1 Worker shared by 1 owner instead of 2 owners. Increasing the number of listeners doesn't affect the ownership.
It has 2 frames, one containing the iframe and one inside the iframe. They both call the
start method of the Worker so it's shared by 2 owners. Since the
start method is called twice, the
onconnect event should be fired twice, thus making
connection.count equal 2.
In shared webworkers the context is alive till the last browser session end. shared webworkers can maintain the context around the browser tabs. They respond to the requests with the same context of data.
The change in context of data will affect all connections, the possibilities are you can update all the connections with single context change, you can maintain the data till the last connection end. you can maintain the connection changes in all views.
Here is a demo of Shared web workers with multiple connections.
<a href="http://www.antkorp.in/sharedworkers/" rel="nofollow">http://www.antkorp.in/sharedworkers/</a>