From the course: JavaScript: Web Workers

Unlock the full course today

Join today to access over 22,700 courses taught by industry experts or purchase this course individually.

Send messages to dedicated workers

Send messages to dedicated workers - JavaScript Tutorial

From the course: JavaScript: Web Workers

Start my 1-month free trial

Send messages to dedicated workers

- [Instructor] One of the most important parts of creating web workers is to communicate the main thread with the web worker and vice versa from the web worker to the mainframe. To do that, we are going to use a postMessage API also available on other APIs inside the browser. So for example, the main thread can execute the postMessage function on the worker variable and it can sent a message, let's say m here. That message will go from the main thread to the web worker and the web worker will receive that message in an event known as message. And the m object, or the data that we are sending, will appear in the event argument in the data article. The web worker can also send data to the mainframe using the same API. It's going to all postMessage on itself, so on the web worker and in the main thread it will arrive also on the message event handler and the data will be available in event.data. Having in mind that we have now a message in API, when we are creating dedicated workers…

Contents