Topic 008: Web workers in JS
Web Workers provide a way to run scripts in background threads. They are useful for performing computationally expensive tasks without blocking the main thread, thereby keeping the UI responsive.
main.js:
// Check if the browser supports web workers
if (window.Worker) {
// Create a new web worker
const worker = new Worker("worker.js");
// Send a message to the worker
worker.postMessage("Hello, worker!");
// Listen for messages from the worker
worker.onmessage = function (event) {
console.log("Message from worker:", event.data);
};
// Handle errors from the worker
worker.onerror = function (error) {
console.error("Worker error:", error);
};
}
worker.js:
// Listen for messages from the main thread
self.onmessage = function (event) {
console.log("Message from main script:", event.data);
// Send a message back to the main thread
self.postMessage("Hello from the worker!");
};
postMessage
and onmessage
.port
objects for message passing, allowing communication between different contexts.postMessage
and onmessage
, similar to web workers, but can also intercept and handle network requests via the fetch
event.Example of a Shared Worker:
main1.js:
const sharedWorker = new SharedWorker("sharedWorker.js");
sharedWorker.port.start();
sharedWorker.port.postMessage("Hello from page 1");
sharedWorker.port.onmessage = function (event) {
console.log("Message from shared worker:", event.data);
};
main2.js:
const sharedWorker = new SharedWorker("sharedWorker.js");
sharedWorker.port.start();
sharedWorker.port.postMessage("Hello from page 2");
sharedWorker.port.onmessage = function (event) {
console.log("Message from shared worker:", event.data);
};
sharedWorker.js:
self.onconnect = function (event) {
const port = event.ports[0];
port.onmessage = function (event) {
console.log("Message from page:", event.data);
port.postMessage("Hello from the shared worker!");
};
};
Example of a Service Worker:
main.js:
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("serviceWorker.js").then(
function (registration) {
console.log("ServiceWorker registration successful with scope: ", registration.scope);
},
function (error) {
console.log("ServiceWorker registration failed: ", error);
}
);
}
serviceWorker.js:
self.addEventListener("install", function (event) {
console.log("Service worker installing...");
// Cache files if needed
});
self.addEventListener("activate", function (event) {
console.log("Service worker activating...");
});
self.addEventListener("fetch", function (event) {
console.log("Fetching:", event.request.url);
event.respondWith(
fetch(event.request).catch(function () {
return new Response("Fallback content if offline");
})
);
});
Each type of worker serves a different purpose and is suited for different use cases within web development.