2

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?


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.

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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 :(
1

You use postMessage as was otherwise indicated here, however, the object / data must be base64 encoded before being passed using atob()

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.