Search code examples
javascriptweb-workershared-worker

What is shared worker? Different between worker vs shared worker?


I had to watch shared worker example. But I can't identify differences between worker vs shared worker. Shared worker example https://github.com/mdn/simple-shared-worker. Can anyone please explain that?


Solution

  • Worker and SharedWorker working flow given below.

    This image helps to understand working flow and common behavior of Worker and SharedWorker

    Difference between Worker and SharedWorker

    Similar features between Worker and Shared Worker.

       The Worker works in another thread. So it's not affecting the main thread. So that time users can scroll, view the page without blocking.
    
    1. Worker can't access DOM elements from the web page.
    2. Worker can't access global variables and functions from the web page.
    3. Worker can't call alert() function.
    4. Objects such as window, parent, document can't be accessed inside the worker.
    

    Workers own features.

    Workers life time between page creates and closure. Once the page creates the new workers will be created. View first image that blog diagram says about Wokers.
    

    Shared Worker own features.

    Shared Worker connects all web pages from same domain. View second image that blog diagram says about Wokers vs Shared Workers.

    Reference link, Workers document