Plotting Fractals in WebAssembly
|6: Zooming In||Top|
|7: WebAssembly and Web Workers||7.2 Schematic Overview|
Web Worker threads behave both in parallel with, and asynchronously from, each other.
Threads exchange Information with each other typically through message passing.1
The messages sent between threads are of an entirely arbitrary structure and may contain any data suitable for your needs.
The code within a worker thread can perform almost any task you require.
The most important exception here is that a worker thread has no access to the browser’s DOM. Therefore, if a worker wants to change the UI, it cannot do this directly. It must send the relevant data back to the main thread, and the main thread then updates the UI.
Workers communicate with the thread that created them by calling their builtin function
postMessageand passing an arbitrary message object.
Any thread that creates a Web Worker needs to attach an event handler function to the worker’s
onmessageevent in order to handle incoming messages from that worker.
In our case however, message passing will only be used to initiate actions and report their completion. The main thread will not receive the actual image data in a message, but through shared memory. ↩