@@ -220,6 +220,118 @@ if (isMainThread) {
220220}
221221```
222222
223+ ## ` worker.postMessageToThread(threadId, value[, transferList][, timeout]) `
224+
225+ <!-- YAML
226+ added: REPLACEME
227+ -->
228+
229+ > Stability: 1.1 - Active development
230+
231+ * ` destination ` {number} The target thread ID. If the thread ID is invalid, a
232+ [ ` ERR_WORKER_MESSAGING_FAILED ` ] [ ] error will be thrown. If the target thread ID is the current thread ID,
233+ a [ ` ERR_WORKER_MESSAGING_SAME_THREAD ` ] [ ] error will be thrown.
234+ * ` value ` {any} The value to send.
235+ * ` transferList ` {Object\[ ] } If one or more ` MessagePort ` -like objects are passed in ` value ` ,
236+ a ` transferList ` is required for those items or [ ` ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST ` ] [ ] is thrown.
237+ See [ ` port.postMessage() ` ] [ ] for more information.
238+ * ` timeout ` {number} Time to wait for the message to be delivered in milliseconds.
239+ By default it's ` undefined ` , which means wait forever. If the operation times out,
240+ a [ ` ERR_WORKER_MESSAGING_TIMEOUT ` ] [ ] error is thrown.
241+ * Returns: {Promise} A promise which is fulfilled if the message was successfully processed by destination thread.
242+
243+ Sends a value to another worker, identified by its thread ID.
244+
245+ If the target thread has no listener for the ` workerMessage ` event, then the operation will throw
246+ a [ ` ERR_WORKER_MESSAGING_FAILED ` ] [ ] error.
247+
248+ If the target thread threw an error while processing the ` workerMessage ` event, then the operation will throw
249+ a [ ` ERR_WORKER_MESSAGING_ERRORED ` ] [ ] error.
250+
251+ This method should be used when the target thread is not the direct
252+ parent or child of the current thread.
253+ If the two threads are parent-children, use the [ ` require('node:worker_threads').parentPort.postMessage() ` ] [ ]
254+ and the [ ` worker.postMessage() ` ] [ ] to let the threads communicate.
255+
256+ The example below shows the use of of ` postMessageToThread ` : it creates 10 nested threads,
257+ the last one will try to communicate with the main thread.
258+
259+ ``` mjs
260+ import { fileURLToPath } from ' node:url' ;
261+ import { once } from ' node:events' ;
262+ import process from ' node:process' ;
263+ import {
264+ isMainThread ,
265+ postMessageToThread ,
266+ threadId ,
267+ workerData ,
268+ Worker ,
269+ } from ' node:worker_threads' ;
270+
271+ const channel = new BroadcastChannel (' sync' );
272+ const level = workerData? .level ?? 0 ;
273+
274+ if (level < 10 ) {
275+ const worker = new Worker (fileURLToPath (import .meta.url), {
276+ workerData : { level : level + 1 },
277+ });
278+ }
279+
280+ if (level === 0 ) {
281+ process .on (' workerMessage' , (value , source ) => {
282+ console .log (` ${ source} -> ${ threadId} :` , value);
283+ postMessageToThread (source, { message: ' pong' });
284+ });
285+ } else if (level === 10 ) {
286+ process .on (' workerMessage' , (value , source ) => {
287+ console .log (` ${ source} -> ${ threadId} :` , value);
288+ channel .postMessage (' done' );
289+ channel .close ();
290+ });
291+
292+ await postMessageToThread (0 , { message: ' ping' });
293+ }
294+
295+ channel .onmessage = channel .close ;
296+ ` ` `
297+
298+ ` ` ` cjs
299+ const { once } = require (' node:events' );
300+ const {
301+ isMainThread ,
302+ postMessageToThread ,
303+ threadId ,
304+ workerData ,
305+ Worker ,
306+ } = require (' node:worker_threads' );
307+
308+ const channel = new BroadcastChannel (' sync' );
309+ const level = workerData? .level ?? 0 ;
310+
311+ if (level < 10 ) {
312+ const worker = new Worker (__filename , {
313+ workerData: { level: level + 1 },
314+ });
315+ }
316+
317+ if (level === 0 ) {
318+ process .on (' workerMessage' , (value , source ) => {
319+ console .log (` ${ source} -> ${ threadId} :` , value);
320+ postMessageToThread (source, { message: ' pong' });
321+ });
322+ } else if (level === 10 ) {
323+ process .on (' workerMessage' , (value , source ) => {
324+ console .log (` ${ source} -> ${ threadId} :` , value);
325+ channel .postMessage (' done' );
326+ channel .close ();
327+ });
328+
329+ postMessageToThread (0 , { message: ' ping' });
330+ }
331+
332+ channel .onmessage = channel .close ;
333+ ` ` `
334+
223335## ` worker .receiveMessageOnPort (port)`
224336
225337<!-- YAML
@@ -1361,6 +1473,10 @@ thread spawned will spawn another until the application crashes.
13611473[` Buffer.allocUnsafe()` ]: buffer .md #static - method- bufferallocunsafesize
13621474[` Buffer` ]: buffer .md
13631475[` ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST` ]: errors .md #err_missing_message_port_in_transfer_list
1476+ [` ERR_WORKER_MESSAGING_ERRORED` ]: errors .md #err_worker_messaging_errored
1477+ [` ERR_WORKER_MESSAGING_FAILED` ]: errors .md #err_worker_messaging_failed
1478+ [` ERR_WORKER_MESSAGING_SAME_THREAD` ]: errors .md #err_worker_messaging_same_thread
1479+ [` ERR_WORKER_MESSAGING_TIMEOUT` ]: errors .md #err_worker_messaging_timeout
13641480[` ERR_WORKER_NOT_RUNNING` ]: errors .md #err_worker_not_running
13651481[` EventTarget` ]: https: // developer.mozilla.org/en-US/docs/Web/API/EventTarget
13661482[` FileHandle` ]: fs .md #class - filehandle
0 commit comments