1

I'm confused as to how to send JSON data back from a webworker to the main thread via an Array Buffer.

suppose:

data = {"key":"value"}

worker.postMessage(data, buffers);

what exactly am I supposed to put in for buffers?

5
  • Can you clarify what you mean to send it "via" an array buffer, and why you want to do that?. Commented May 17, 2015 at 10:06
  • please see here: html5rocks.com/en/tutorials/workers/basics and here updates.html5rocks.com/2011/12/… Commented May 17, 2015 at 14:19
  • You have an JSON object in a web worker, and would like to transfer (not copy) it to the main thread? Commented May 17, 2015 at 14:28
  • yes exactly, i'm not sure of the exact argument in the postMessage() Commented May 17, 2015 at 15:02
  • I suspect that some copying may be unavoidable: according to developer.mozilla.org/en-US/docs/Web/API/Transferable you can only transfer ArrayBuffer and MessagePort objects. You could potentially "pack" a JSON object into an ArrayBuffer, using something like github.com/theangryangel/jspack-arraybuffer, but I suspect it'll be more efficient to just copy the JSON object between the threads, rather than copy/pack it into an ArrayBuffer to then transfer. Commented May 17, 2015 at 15:07

1 Answer 1

1

.postMessage accepts a message identifier and the message data like this:

var message_data = {'key':'val'};
worker.postMessage('my_message', message_data);

message identifier in a sense signifies the type of message or event sent, then the data are the actual message data or parameters, which can include any JS transferable type (including Typed Arrays)

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

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.