What does the message event do in a worker?
What the message event does
The
messageevent fires when a thread (Worker) receives data viapostMessage(), either from the main thread or from another worker.
In other words:
messageis the "data received" event.
1. Thread interaction
In Node.js, the worker_threads module provides:
Worker(in the main thread)- and
parentPort(inside the worker)
They can send and receive messages:
worker.postMessage(), send a message to the threadworker.on('message'), get a reply from the threadparentPort.postMessage(), send a message back to the main threadparentPort.on('message'), receive a message from it
2. An example: exchanging messages
main.js
const { Worker } = require('worker_threads');
const worker = new Worker('./worker.js');
// 1. Send data to the thread
worker.postMessage({ name: 'Tim', numbers: [1, 2, 3] });
// 2. Listen for the reply from the thread
worker.on('message', (result) => {
console.log('Reply from the worker:', result);
});worker.js
const { parentPort } = require('worker_threads');
// 3. Listen for the incoming message
parentPort.on('message', (data) => {
const sum = data.numbers.reduce((a, b) => a + b, 0);
// 4. Send the result back
parentPort.postMessage({ greeting: `Hello, ${data.name}!`, sum });
});Here:
main.jscallsworker.postMessage()→ the thread receives the message.- The thread's
messageevent fires → we process the data. - The thread then sends the result back → the main thread's
worker.on('message')fires.
3. The data flow
Main thread (main)
│
│ worker.postMessage(data)
▼
Worker (worker)
│
│ parentPort.on('message', handler)
▼
Processing data...
│
│ parentPort.postMessage(result)
▼
Main thread receives it → worker.on('message', handler)4. An example of two-way communication
main.js
const { Worker } = require('worker_threads');
const worker = new Worker('./worker.js');
worker.on('message', msg => {
console.log('Message from the worker:', msg);
if (msg === 'done') worker.terminate();
});
worker.postMessage('start working');worker.js
const { parentPort } = require('worker_threads');
parentPort.on('message', (msg) => {
if (msg === 'start working') {
console.log('The worker got the command');
parentPort.postMessage('done');
}
});Here the main thread and the worker exchange messages in both directions.
5. The format of the data passed
postMessage() can pass:
- any serializable data (objects, arrays, strings, numbers),
- ArrayBuffer, TypedArray, SharedArrayBuffer,
- and even Transferable objects (passed without copying).
6. Worth remembering
- The
messageevent fires every time a new message arrives. - Threads have no shared memory (unless
SharedArrayBufferis used). - Exchange happens via message passing (messages, not shared variables).
- Objects are passed by value (copied), unless Transferable objects are used.
7. Quick summary
| Element | Used where | Purpose |
|---|---|---|
worker.on('message', cb) | In the main thread | Receiving messages from the worker |
worker.postMessage(data) | In the main thread | Sending data to the worker |
parentPort.on('message', cb) | Inside the worker | Receiving messages from the main thread |
parentPort.postMessage(data) | Inside the worker | Sending messages back |
Summary
| Characteristic | Description |
|---|---|
What message does | Fires when a thread receives data |
| Where it's used | Inside a worker (parentPort.on('message')) or the main thread (worker.on('message')) |
| How to send data | Via postMessage() |
| Data transfer | Via serialization or Transferable objects |
| Direction | Two-way (main ↔ worker) |
| Use case | Communication between threads with no shared memory |
Conclusion:
The
messageevent is how threads "talk" to each other in Node.js. When one thread callspostMessage(), the other receives that message through itson('message')handler.This is the foundation of asynchronous data exchange between the main thread and workers.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.