M
worker_threads.receiveMessageOnPort
History
Added in: v12.3.0
v12.3.0
Introduced in: v10.5.0
v10.5.0
The port argument can also refer to a
BroadcastChannel now.v15.12.0
worker_threads.receiveMessageOnPort(port): Object | undefined
Attributes
port:
MessagePort | BroadcastChannelReceive a single message from a given MessagePort. If no message is available,
undefined is returned, otherwise an object with a single message property
that contains the message payload, corresponding to the oldest message in the
MessagePort's queue.
import { MessageChannel, receiveMessageOnPort } from 'node:worker_threads'; const { port1, port2 } = new MessageChannel(); port1.postMessage({ hello: 'world' }); console.log(receiveMessageOnPort(port2)); // Prints: { message: { hello: 'world' } } console.log(receiveMessageOnPort(port2)); // Prints: undefined
const { MessageChannel, receiveMessageOnPort } = require('node:worker_threads'); const { port1, port2 } = new MessageChannel(); port1.postMessage({ hello: 'world' }); console.log(receiveMessageOnPort(port2)); // Prints: { message: { hello: 'world' } } console.log(receiveMessageOnPort(port2)); // Prints: undefined
When this function is used, no 'message' event is emitted and the
onmessage listener is not invoked.