Setting up a shared worker

A shared worker can be created by calling the SharedWorker constructor and providing the name of the file as the argument:

const awesomeworker = new SharedWorker('myworker.js');

Here, we used the SharedWorker constructor to create an instance of a sharedworker object. Unlike with dedicated workers, you won't be able to see the HTTP network request in the browser made to the myworker.js file. This is important because the browser has to maintain only one instance of this file across multiple scripts calling this web worker:

// myworker.js
console.log('Hello world!');

Unlike dedicated workers, this does not log Hello World! in the main website's console. This is because shared workers do not get loaded into only that page. A shared worker is loaded once for every file accessing it. Therefore, it has its own console.

In Google Chrome, to debug a shared worker, open chrome://inspect/#workers after opening the page which is responsible for launching the shared worker. There, you'll have the option to debug it: ("Inspect" link)

With that done, let's proceed to the guide to setting up listeners on shared workers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.35.148