Yesterday, I was introduced to Javascript Web Workers during a bad-ass FireBug console hack-session with Al MacDonald (http://hyper-metrix.com). I have to say… this is the coolest thing I’ve seen in a very long time… Workers are currently available in FireFox 3.5, Safari 4 and (according to John Resig) the Chromium Nightlies.
I’d like to consider this article more of an “advanced” web workers topic, so I’ll skip the explanation of the basic introduction to web workers and simply give you the files to begin with: download workers-simple.zip
Unzip this and drop it into your locahost. Open all the files and review them. If you want more information about what you’re looking at, have a gander at this. If you’re ready to really kick out the jam, then keep reading.
You might have already been saying to yourself “what the hell can I do with this in the real world?” and the answer is not much – except for reporting the current time over and over again. So let’s turbo-charge this worker.
First things first… scripts called by the Worker object do have XMLHttpRequest, but who wants to copy/paste or rewrite their XHR methods into EVERY worker script? Not me, that’s for sure. But now we’re faced with the challenge of getting external files into our worker script. This part is shockingly simple. Ready? GO!
- Open index.php:
- Line 9, replace:
1
var worker = new Worker('worker.js');…with…
1
var worker = new Worker('worker.js.php'); - Line 14, add:
1 2 3 4 5 6
$(document).ready(function () { $('#post') .click(function () { worker.postMessage('load'); }); });
- Line 9, replace:
- Open worker.js and at the top of the file, add this:
1 2 3
<?php header('Content-type: text/javascript'); ?>
- Now, save-as “worker.js.php”.
Great! Now let’s add a small ajax library… (get that here)
- In worker.js.php, add an include to ajax.js below the header() call, it will look like this:
1 2 3 4
<?php header('Content-type: text/javascript'); include('ajax.js'); ?>
- At the bottom of the script, add in the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Recieve data FROM the client with postMessage() onmessage = function(event){ if ( event.data === 'load' ) { postMessage( '-----BEGIN TRANSMISSION-----'); postMessage( {'server_objects': this} ); $.ajax.get({ url: 'xhr_test_content.txt', callback: function(response) { var text = response.text; postMessage( 'AJAX GET RETURNED: ' + text ); } }); postMessage( '-----END TRANSMISSION-------'); } };
Back to index.php…
- Inside the body, add this:
1
<input type=button id="post" value="post back to server">
Create a new file called “xhr_test_content.txt” And put whatever you want in it (or… challenge yourself and make it a PHP file that gets some kind of data from a db… )
Lastly… open your new index.php file in FireFox 3.5, fire up the console and let it rip. Click your new button to see what it returns.
If I missed something in my steps, you can always download the finished the code and run it yourself in its complete form. Download Completed Source Here
4 Comments
Shouldn’t this also work with Safari 4? If so, why limit to just Firefox 3.5?
not sure i understand why this is way cool, its simply some dynamically created scripts, and some post messages…
pretty standard stuff really.
i gotta admit, once i began working on web workers, i did share the same initial response tho….
WAY COOL!
@GG
I think it’s way cool.
1. There are no dynamically created scripts.
2. postMessage() is a method of the Worker object, which only debuted as part of HTML5 and then was broken off into it’s own specification.
3. It still remains… WAY COOL
Keep in mind that importScripts() exists… This example is to show instead how it can be compiled into one file. The method shown here has security advantages.
10 Trackbacks
[...] Over in Geneva, they have posted on coupling PHP and Workers. [...]
[...] Over in Geneva, they have posted on coupling PHP and Workers. [...]
[...] Over in Geneva, they have posted on coupling PHP and Workers. [...]
[...] Over in Geneva, they have posted on coupling PHP and Workers. [...]
[...] GenevaJS [...]
[...] Over in Geneva, they have posted on coupling PHP and Workers. [...]
[...] Over in Geneva, they have posted on coupling PHP and Workers. [...]
[...] the past couple of months, there’s been some good information floating around about web workers. I have no desire to add yet another introduction to the topic into the [...]
[...] link: Expanding Capabilities of Javascript Web Workers Share and [...]
[...] a follow up to my previous post regarding Javascript Web Workers, I’m pleased to announce the release of PollenJS, the first worker-specific javascript [...]