@@ -252,6 +252,118 @@ if (isMainThread) {
252252}
253253```
254254
255+ ## ` worker.postMessageToThread(threadId, 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. If the thread ID is invalid, a
264+ [ ` ERR_WORKER_MESSAGING_FAILED ` ] [ ] error will be thrown. If the target thread ID is the current thread ID,
265+ a [ ` ERR_WORKER_MESSAGING_SAME_THREAD ` ] [ ] error will be thrown.
266+ * ` value ` {any} The value to send.
267+ * ` transferList ` {Object\[ ] } If one or more ` MessagePort ` -like objects are passed in ` value ` ,
268+ a ` transferList ` is required for those items or [ ` ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST ` ] [ ] is thrown.
269+ See [ ` port.postMessage() ` ] [ ] for more information.
270+ * ` timeout ` {number} Time to wait for the message to be delivered in milliseconds.
271+ By default it's ` undefined ` , which means wait forever. If the operation times out,
272+ a [ ` ERR_WORKER_MESSAGING_TIMEOUT ` ] [ ] error is thrown.
273+ * Returns: {Promise} A promise which is fulfilled if the message was successfully processed by destination thread.
274+
275+ Sends a value to another worker, identified by its thread ID.
276+
277+ If the target thread has no listener for the ` workerMessage ` event, then the operation will throw
278+ a [ ` ERR_WORKER_MESSAGING_FAILED ` ] [ ] error.
279+
280+ If the target thread threw an error while processing the ` workerMessage ` event, then the operation will throw
281+ a [ ` ERR_WORKER_MESSAGING_ERRORED ` ] [ ] error.
282+
283+ This method should be used when the target thread is not the direct
284+ parent or child of the current thread.
285+ If the two threads are parent-children, use the [ ` require('node:worker_threads').parentPort.postMessage() ` ] [ ]
286+ and the [ ` worker.postMessage() ` ] [ ] to let the threads communicate.
287+
288+ The example below shows the use of of ` postMessageToThread ` : it creates 10 nested threads,
289+ the last one will try to communicate with the main thread.
290+
291+ ``` mjs
292+ import { fileURLToPath } from ' node:url' ;
293+ import { once } from ' node:events' ;
294+ import process from ' node:process' ;
295+ import {
296+ isMainThread ,
297+ postMessageToThread ,
298+ threadId ,
299+ workerData ,
300+ Worker ,
301+ } from ' node:worker_threads' ;
302+
303+ const channel = new BroadcastChannel (' sync' );
304+ const level = workerData? .level ?? 0 ;
305+
306+ if (level < 10 ) {
307+ const worker = new Worker (fileURLToPath (import .meta.url), {
308+ workerData : { level : level + 1 },
309+ });
310+ }
311+
312+ if (level === 0 ) {
313+ process .on (' workerMessage' , (value , source ) => {
314+ console .log (` ${ source} -> ${ threadId} :` , value);
315+ postMessageToThread (source, { message: ' pong' });
316+ });
317+ } else if (level === 10 ) {
318+ process .on (' workerMessage' , (value , source ) => {
319+ console .log (` ${ source} -> ${ threadId} :` , value);
320+ channel .postMessage (' done' );
321+ channel .close ();
322+ });
323+
324+ await postMessageToThread (0 , { message: ' ping' });
325+ }
326+
327+ channel .onmessage = channel .close ;
328+ ` ` `
329+
330+ ` ` ` cjs
331+ const { once } = require (' node:events' );
332+ const {
333+ isMainThread ,
334+ postMessageToThread ,
335+ threadId ,
336+ workerData ,
337+ Worker ,
338+ } = require (' node:worker_threads' );
339+
340+ const channel = new BroadcastChannel (' sync' );
341+ const level = workerData? .level ?? 0 ;
342+
343+ if (level < 10 ) {
344+ const worker = new Worker (__filename , {
345+ workerData: { level: level + 1 },
346+ });
347+ }
348+
349+ if (level === 0 ) {
350+ process .on (' workerMessage' , (value , source ) => {
351+ console .log (` ${ source} -> ${ threadId} :` , value);
352+ postMessageToThread (source, { message: ' pong' });
353+ });
354+ } else if (level === 10 ) {
355+ process .on (' workerMessage' , (value , source ) => {
356+ console .log (` ${ source} -> ${ threadId} :` , value);
357+ channel .postMessage (' done' );
358+ channel .close ();
359+ });
360+
361+ postMessageToThread (0 , { message: ' ping' });
362+ }
363+
364+ channel .onmessage = channel .close ;
365+ ` ` `
366+
255367## ` worker .receiveMessageOnPort (port)`
256368
257369<!-- YAML
@@ -1399,6 +1511,10 @@ thread spawned will spawn another until the application crashes.
13991511[` Buffer.allocUnsafe()` ]: buffer .md #static - method- bufferallocunsafesize
14001512[` Buffer` ]: buffer .md
14011513[` ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST` ]: errors .md #err_missing_message_port_in_transfer_list
1514+ [` ERR_WORKER_MESSAGING_ERRORED` ]: errors .md #err_worker_messaging_errored
1515+ [` ERR_WORKER_MESSAGING_FAILED` ]: errors .md #err_worker_messaging_failed
1516+ [` ERR_WORKER_MESSAGING_SAME_THREAD` ]: errors .md #err_worker_messaging_same_thread
1517+ [` ERR_WORKER_MESSAGING_TIMEOUT` ]: errors .md #err_worker_messaging_timeout
14021518[` ERR_WORKER_NOT_RUNNING` ]: errors .md #err_worker_not_running
14031519[` EventTarget` ]: https: // developer.mozilla.org/en-US/docs/Web/API/EventTarget
14041520[` FileHandle` ]: fs .md #class - filehandle
0 commit comments