Sending messages from the worker script

If you've recognized the difference between how we call methods in dedicated workers versus how we call them in shared workers, well done! Instead of just calling methods on self, we're calling all the dedicated web worker methods on the port object, which is how the worker distinguishes between so many scripts that (can possibly) talk to it:

// myworker.jsaddEventListener('connect', e => {    console.log(e.ports);    const port = e.ports[0];    port.start();    port.addEventListener('message', event => {        console.log('Some calling script says.. ', event.data);         // some work        port.postMessage("Hello ;)");    });});

It is exactly like the code above, but with the exception that this time our shared worker replies to ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.