Dedicated Worker Global Scope

With DedicatedWorkerGlobalScope it gives you direct interaction with both sides of worker/main thread.


const worker = new Worker("/worker.js");
worker.postMessage("hello from main");

// now we can listen for messages from the DedicatedWorkerGlobalScope

// note we are adding the event listener to the worker
worker.addEventListener("message", (e) => {
  console.log("message from worker",;


onmessage = (e) => {
  console.log("message from main",;
  // note we are using self here
  self.postMessage("hello from worker");