I need to share an object between a client side application and a web worker and I heard about SharedArrayBuffers. This is what I'd like to do.
main.js
let myWorker = new Worker('/worker.js')
let mySab = new SharedArrayBuffer(1024)
let myObj = { foo: 'bar', bar: 'foo' }
// Save 'myObj' to 'mySab'
worker.postMessage(sab)
worker.js
self.onmessage = (e) => {
let myObj = BLANK // Get 'myObj' from SharedArrayBuffer
}
Is this possible? The examples I've seen of ShareArrayBuffers only ever save numbers in the buffer. Any help is appreciated!
No, SharedArrayBuffers only store binary data. You can only send copies of objects via the built-in postMessage function.
Sources:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With