Skip to content

Instantly share code, notes, and snippets.

@leon0707
Last active February 4, 2020 14:52
Show Gist options
  • Save leon0707/13001e2c9923fad7b5e8c948579970d8 to your computer and use it in GitHub Desktop.
Save leon0707/13001e2c9923fad7b5e8c948579970d8 to your computer and use it in GitHub Desktop.
shared worker
// shared-worker.js
onconnect = function(e) {
var port = e.ports[0];
port.onmessage = function(e) {
port.postMessage('send msg back to a script');
}
}
// client
if (!!window.SharedWorker) {
const myWorker = new SharedWorker("shared-worker.js");
myWorker.port.postMessage('msg to a shared worker');
myWorker.port.onmessage = function(e) {
console.log(e.lastEventId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment