@@ -252,6 +252,111 @@ if (isMainThread) {
252252}
253253```
254254
255+ ## ` worker.postMessageToWorker(destination, value[, transferList][, timeout]) `
256+
257+ <!-- YAML
258+ added: REPLACEME
259+ -->
260+
261+ > Stability: 1.1 - Active development
262+
263+ * ` destination ` {number} The target thread ID.
264+ * ` value ` {any} The value to send.
265+ * ` transferList ` {Object\[ ] } If one or more ` MessagePort ` -like objects are passed in ` value ` ,
266+ a ` transferList ` is required for those items or [ ` ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST ` ] [ ] is thrown.
267+ See [ ` port.postMessage() ` ] [ ] for more information.
268+ * ` timeout ` {number} Time to wait for the message to be delivered in milliseconds.
269+ By default it's ` undefined ` , which means wait forever.
270+ * Returns: {Promise} A promise which is fulfilled if the message was successfully sent.
271+
272+ Sends a value to another worker, identified by its thread ID.
273+
274+ If the target thread has no listener for the ` workerMessage ` event, then the operation will throw an error.
275+
276+ This method should be used when the target thread is not the direct
277+ parent or child of the current thread.
278+ If the two threads are parent-children, use the [ ` require('node:worker_threads').parentPort.postMessage() ` ] [ ]
279+ and the [ ` worker.postMessage() ` ] [ ] to let the threads communicate.
280+
281+ The example below shows the use of of ` postMessageToWorker ` : it creates 10 nested threads,
282+ the last one will try to communicate with the main thread.
283+
284+ ``` mjs
285+ import { fileURLToPath } from ' node:url' ;
286+ import { once } from ' node:events' ;
287+ import process from ' node:process' ;
288+ import {
289+ isMainThread ,
290+ postMessageToWorker ,
291+ threadId ,
292+ workerData ,
293+ Worker ,
294+ } from ' node:worker_threads' ;
295+
296+ const channel = new BroadcastChannel (' sync' );
297+ const level = workerData? .level ?? 0 ;
298+
299+ if (level < 10 ) {
300+ const worker = new Worker (fileURLToPath (import .meta.url), {
301+ workerData : { level : level + 1 },
302+ });
303+ }
304+
305+ if (level === 0 ) {
306+ process .on (' workerMessage' , (value , source ) => {
307+ console .log (` ${ source} -> ${ threadId} :` , value);
308+ postMessageToWorker (source, { message: ' pong' });
309+ });
310+ } else if (level === 10 ) {
311+ process .on (' workerMessage' , (value , source ) => {
312+ console .log (` ${ source} -> ${ threadId} :` , value);
313+ channel .postMessage (' done' );
314+ channel .close ();
315+ });
316+
317+ await postMessageToWorker (0 , { message: ' ping' });
318+ }
319+
320+ channel .onmessage = channel .close ;
321+ ` ` `
322+
323+ ` ` ` cjs
324+ const { once } = require (' node:events' );
325+ const {
326+ isMainThread ,
327+ postMessageToWorker ,
328+ threadId ,
329+ workerData ,
330+ Worker ,
331+ } = require (' node:worker_threads' );
332+
333+ const channel = new BroadcastChannel (' sync' );
334+ const level = workerData? .level ?? 0 ;
335+
336+ if (level < 10 ) {
337+ const worker = new Worker (__filename , {
338+ workerData: { level: level + 1 },
339+ });
340+ }
341+
342+ if (level === 0 ) {
343+ process .on (' workerMessage' , (value , source ) => {
344+ console .log (` ${ source} -> ${ threadId} :` , value);
345+ postMessageToWorker (source, { message: ' pong' });
346+ });
347+ } else if (level === 10 ) {
348+ process .on (' workerMessage' , (value , source ) => {
349+ console .log (` ${ source} -> ${ threadId} :` , value);
350+ channel .postMessage (' done' );
351+ channel .close ();
352+ });
353+
354+ postMessageToWorker (0 , { message: ' ping' });
355+ }
356+
357+ channel .onmessage = channel .close ;
358+ ` ` `
359+
255360## ` worker .receiveMessageOnPort (port)`
256361
257362<!-- YAML
0 commit comments