Workers
worker_threads.receiveMessageOnPort()
Reads one queued message synchronously, or returns undefined when no message is ready. Do not spin in a busy loop waiting for data. Tested on Node.js 22.16.0. Example output (one line per entry): ["true", "ready"].
Formal Syntax & Structure
nodejs syntaxnodejs
receiveMessageOnPort(port)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| port | MessagePort | BroadcastChannel | Receiving endpoint. |
Return Value
object | undefined
Message wrapper or absence.
Working Example
Example Snippetnodejs
import { MessageChannel, receiveMessageOnPort } from 'node:worker_threads';const { port1, port2 } = new MessageChannel();try { console.log(receiveMessageOnPort(port2) === undefined); port1.postMessage('ready'); console.log(receiveMessageOnPort(port2).message);}finally { port1.close(); port2.close();}