Friday, December 5th, 2008

Web Workers: Tweaked and out in Firefox 3.1b2

Category: HTML

Ben Turner posted on Web Workers being available in the upcoming beta2 using the latest spec:

First, we’ve moved away from the old Gears-esque API that we had in Alpha 2 (see previous post) in favor of the emerging Web Workers standard. We’ve been hard at work with folks from Google, Apple, and others to get this new spec nailed down.

Most of the missing features I identified in Part 1 have been added with the exception of auto-JSON. There will be a very few small changes between Beta 2 and Firefox 3.1 final (most of which have already landed) and I’ll post about those again once we’re close to shipping.

So what’s available in Beta 2? We currently implement this subset of the Web Workers spec. Very briefly, creating a new worker is as simple as adding the following line to your web page’s script:

var worker = new Worker("workerScript.js");

Your workerScript.js file can do almost anything, really, including using XMLHttpRequest, importing additonal scripts, spawning new workers, setting timeouts/intervals, and passing complex messages via JSON. Further information can be found in Using DOM Workers.

And, in case anyone is interested in a more real-world example, I ported Chris Double’s JavaScript 8080 emulator to use Workers. The nice thing about using workers here is that all the actual emulation is happening on a background thread (or, on my machine, a second CPU). Keystrokes are sent as messages to the emulator thread and then emulator sends screen updates back (all using JSON). It also uses the importScripts function to load Chris’s hosted files rather than having to copy them around. Note that I didn’t bother with very serious error checking so unless you’re using Firefox 3 Beta 2 or newer then it most likely won’t work at all. Check it out here.

It is interesting to crack open the spec an see how it has been updated. It fills really well integrated with other parts of HTML 5. You can share workers cleanly, open a local database directly, show notifications, and import Scripts. Looks really nice.

Posted by Dion Almaer at 10:18 am
1 Comment

+++--
3.2 rating from 57 votes

1 Comment »

Comments feed TrackBack URI

I like the idea of workers, but on thing worries me:

Your workerScript.js file can do almost anything, really, including using XMLHttpRequest, importing additonal scripts, spawning new workers, setting timeouts/intervals…

Are fork bombs coming to the browser now?

Comment by tj111 — December 5, 2008

Leave a comment

You must be logged in to post a comment.