views:

100

answers:

4

I'm building a multi-threaded client side javascript application, and I would like to have a background thread pull binary data and pass it to the main thread. I know this can be done in other languages via serialization, but how do I accomplish this in javascript?

A: 

Are you open to using Google Gears? This is exactly the kind of thing their WorkerPool threading implementation is for.

Matthew Flaschen
+1  A: 

I might turn this application into a standalone XULrunner application down the line for even more efficiency, so I'd rather go the HTML5 "web workers" route versus using Gears.

gmiernicki
+2  A: 

The Web Workers postMessage API takes a JavaScript object. The Mozilla Using web workers documentations says:

You can safely pass objects in and out of workers using the postMessage() method; objects are automatically converted to JSON internally.

So, you can use any JavaScript object that supports or can be converted to your binary data. Depending on other factors, maybe convert to a Base64 string (see How can you encode to Base64 using Javascript?) or use an array on numbers.

Kevin Hakanson
thanks for the answer! I'd vote this answer up but, dont have the reputation to do so yet.I was under the impression that only strings were allowed as it was documented as "messaging" but from your explanation, it seems that it can handle a lot more. Also, thanks for the link.
gmiernicki
Actually, I've done more research on this topic and it appears the JSON encoding that Firefox does appears to work on 3.6+. Not even in the released 3.5 version of Firefox.I've thought to follow the other approach of using window.atob(), but this method is not available in the web workers themselves as they cannot access the DOM.I can get my application to work in 3.6+ builds of Firefox, but that doesn't do much for me as only 3.5 is out today :(
gmiernicki
A: 

I just wrote a blog post about this crippling feature: http://www.picnet.com.au/blogs/Guido/post/2010/02/18/Html-Web-Worker-Woes-Data-Analysis-not-an-Option.aspx

You may find it interesting.

Thanks

Guido

gatapia
Nice article. Thanks.
Alan