Web Workers are a way that JavaScript has to start running tasks on a new thread. This helps to offload some heavy processing for your web pages.
There are really only two things you need for web workers.
worker.postMessage( data )
worker.addEventListener('message', func) If you need to shut down a worker you can call the self.close( ) method from within the worker or the worker.terminate( ) method from the initiating script.
To send commands to and from service worker involves
postMessage(string || object);
addEventListener('message', doSomething());Arguments of postMessage can be strings, objects and etc. Function that runs when web worker gets message.
The object's message is always in .data property of object.
The method postMessage() sends message back to calling script.