0
0
Node.jsframework~10 mins

Creating worker threads in Node.js - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating worker threads
Main Thread Starts
Create Worker Thread
Worker Thread Runs Code
Worker Sends Message to Main
Main Receives Message
Main and Worker Continue or Exit
The main thread creates a worker thread that runs code separately. They communicate by sending messages back and forth.
Execution Sample
Node.js
import { Worker } from 'node:worker_threads';

const worker = new Worker(`
  const { parentPort } = require('worker_threads');
  parentPort.postMessage('Hello from worker');
`, { eval: true });

worker.on('message', msg => console.log(msg));
This code creates a worker thread that sends a message to the main thread, which then logs it.
Execution Table
StepActionThreadMessage SentMessage ReceivedOutput
1Main thread startsMain
2Create worker thread with inline codeMain
3Worker thread starts running codeWorker
4Worker sends message 'Hello from worker'WorkerHello from worker
5Main thread receives messageMainHello from worker
6Main thread logs messageMainHello from worker
7Both threads continue or exitMain & Worker
💡 Execution stops after message is logged and no more code runs.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5Final
workerundefinedWorker instance createdWorker runningMessage received event setWorker instance exists
Key Moments - 3 Insights
Why does the worker code run separately from the main thread?
Because the worker thread runs its own event loop and code independently, as shown in steps 3 and 4 where the worker sends a message without blocking the main thread.
How does the main thread get the message from the worker?
The main thread listens for the 'message' event on the worker object, as shown in step 5 where it receives 'Hello from worker'.
What does the { eval: true } option do when creating the worker?
It allows passing the worker code as a string to be evaluated, instead of loading from a separate file, as seen in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the worker send a message to the main thread?
AStep 2
BStep 4
CStep 5
DStep 3
💡 Hint
Check the 'Message Sent' column in the execution table.
According to the variable tracker, what is the state of the 'worker' variable after step 2?
AUndefined
BMessage received event set
CWorker instance created
DWorker running
💡 Hint
Look at the 'After Step 2' column for 'worker' in the variable tracker.
If the main thread did not listen for messages, what would happen in the execution table?
AStep 5 would not show a message received
BStep 4 would not send a message
CStep 6 would log the message anyway
DWorker thread would not start
💡 Hint
Refer to the 'Message Received' column in the execution table.
Concept Snapshot
Creating worker threads in Node.js:
- Use 'Worker' from 'worker_threads' module
- Pass code or file to run in worker
- Workers run code separately from main thread
- Communicate via 'postMessage' and 'message' events
- Use { eval: true } to run inline code
- Main thread listens for messages to receive data
Full Transcript
In Node.js, creating worker threads lets you run code in parallel without blocking the main thread. The main thread creates a Worker instance, passing code to run. The worker runs independently and can send messages back to the main thread using postMessage. The main thread listens for these messages with the 'message' event. This allows communication between threads. Using the { eval: true } option lets you pass code as a string instead of a file. This example shows the main thread creating a worker that sends a greeting message, which the main thread logs. This way, heavy or blocking tasks can run in the worker without freezing the main program.