Suggest an editImprove this articleRefine the answer for “What does the message event do in a worker?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)The `message` event fires when a thread receives data through `postMessage()`, whether from the main thread or from another worker - it's essentially a "data received" event. **Key point:** exchange happens through serialization (a copy) of the data, unless Transferable objects are used.Shown above the full answer for quick recall.Answer (EN)Image## 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 | 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 `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.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.