Skip to main content

What does the message event do in a worker?

What the message event does

The message event fires when a thread (Worker) receives data via postMessage(), either from the main thread or from another worker.

In other words:

message is 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 thread
  • worker.on('message'), get a reply from the thread
  • parentPort.postMessage(), send a message back to the main thread
  • parentPort.on('message'), receive a message from it

2. An example: exchanging messages

main.js

javascript
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

javascript
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.js calls worker.postMessage() → the thread receives the message.
  • The thread's message event fires → we process the data.
  • The thread then sends the result back → the main thread's worker.on('message') fires.

3. The data flow

javascript
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

javascript
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

javascript
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 message event fires every time a new message arrives.
  • Threads have no shared memory (unless SharedArrayBuffer is used).
  • Exchange happens via message passing (messages, not shared variables).
  • Objects are passed by value (copied), unless Transferable objects are used.

7. Quick summary

ElementUsed wherePurpose
worker.on('message', cb)In the main threadReceiving messages from the worker
worker.postMessage(data)In the main threadSending data to the worker
parentPort.on('message', cb)Inside the workerReceiving messages from the main thread
parentPort.postMessage(data)Inside the workerSending messages back

Summary

CharacteristicDescription
What message doesFires when a thread receives data
Where it's usedInside a worker (parentPort.on('message')) or the main thread (worker.on('message'))
How to send dataVia postMessage()
Data transferVia serialization or Transferable objects
DirectionTwo-way (main ↔ worker)
Use caseCommunication between threads with no shared memory

Conclusion:

The message event is how threads "talk" to each other in Node.js. When one thread calls postMessage(), the other receives that message through its on('message') handler.

This is the foundation of asynchronous data exchange between the main thread and workers.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.