Server-side threading with worker threads
Now let’s take a look at the server side, using Node.js. In this case, instead of web workers, we use the concept of a worker thread. A worker thread is similar to a web worker in that we pass messages back and forth from the main thread to the worker.
For example, let’s say we have two files, main.js
and worker.js
. We’ll run main.js
(using the command: node main.js
) and it will spawn a thread by loading worker.js
as a worker thread. Here is our main.js
file:
const { Worker } = require('worker_threads');
function fetchPersonWithWorker(id) {
return new Promise((resolve, reject) => {
const worker = new Worker('./worker.js', { workerData: id });
worker.on('message', (data) => {
if (data.error) {
reject(data.error);
} else {
resolve(data);
}
worker.terminate();
});
worker.on('error', (error) => reject(error));
let url = `https://swapi.dev/api/people/${id}`;
worker.postMessage({ url });
});
}
const lukeId = 1;
const leiaId = 5;
console.log("Fetching Star Wars characters with worker threads...");
Promise.all([fetchPersonWithWorker(lukeId), fetchPersonWithWorker(leiaId)])
.then(data => {
console.log("Characters received: "+ JSON.stringify(data) );
console.log(data[0]); // Data for Luke Skywalker (ID: 1)
console.log(data[1]); // Data for Leia Organa (ID: 5)
})
.catch(error => console.error("Error fetching characters:", error));
console.log("Moving on to other things...");
We import Worker
from the worker_threads
module, but note that it’s built into Node, so we don’t need NPM for this. To launch the worker, we create a new Worker
object and give it the worker.js
file as a parameter. Once that is done, we add a message listener that resolves or rejects our promise—this is exactly like we did for the web worker. We also terminate the worker when done, to clean up the resources.