Web Worker API

.Javascript is single thread language means if we have multi-task to performed on the page then it only does single task at a time and after that task, its do the second task, other words its only process single task at a time.

what if we want to perform multi-task on the web?
   luckily, we have an API for it, which is "Web Worker API". This WW(web worker API) API is used to process a second script on the background of the webpage.

WW API using Example:
    In Facebook, we are doing lots of work like share, post, chat, upload, like, etc. but they performed all operation simultaneously on the background of the webpage.
   let's suppose you got a notification on your profile, So how its process that notification and they give to you because you doing your work in a webpage or on your profile.
   So this operation is performed with the help of WW API.
whenever facebook process any operation then they throw that operation in a second script (WW script), so that second script or worker script is getting any notification for your profile then they postMessage() to the main page or main thread which shows you on your profile.
    So that's how we work in WW API to do multiple tasks simultaneously.


Types of WW API:
Normally, we have two types of WW such as DEDICATED WORKER AND SHARED WORKER.

A worker is an object created using a constructor e.g Worker(), that's run a javascript code on a worker thread.

In Dedicated worker, the worker is utilized by a single script and in the Shared worker, they shared between multiple scripts.

The worker basically communicates with the main page using postMessage() method and onmessage event.
In the postMessage() method, we send the data from the main page to the worker page.
In the onmessage event, when the worker received a message or data from the main page using postMessage() method, then the onmessage event is received that data and then we store or do any operation with that data.
    After that, when the operation is successfully completed then the worker thread or page it sends data with postMessage() method to the main page and the main page or thread is received that data using onmessage event to show that data to a user or do any operation with that data.

Whenever we want to use the WW API. first of all, we have to check that the browser is supported WW API or not.
    If it's supported then we undefined the existing WW to create new WW.
    Then, we have to create an object, using constructor "Worker()" to gives the worker page file path in a constructor.
So, its successfully get the file of worker page and we have to send a message via postMessage() method and received a message via onmessage event.
Workers may use XMLHttpRequest for network I/O, with the exception that the responseXML and channel attributes on XMLHttpRequest always return null.

RESOURCES:
  • If you want to see a basic dedicated worker example (code) by MDN.
  • If you want to learn or use WW API.
  • Watch video.

Comments

Popular posts from this blog

How Network Communications Occurs? | OSI Model.

Languages for AI

Window vs Linux for Android Development.