• port {MessagePort}

    • Returns: {Object|undefined}

    Receive 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.

    1. const { MessageChannel, receiveMessageOnPort } = require('worker_threads');
    2. const { port1, port2 } = new MessageChannel();
    3. port1.postMessage({ hello: 'world' });
    4. console.log(receiveMessageOnPort(port2));
    5. // Prints: { message: { hello: 'world' } }
    6. console.log(receiveMessageOnPort(port2));
    7. // Prints: undefined

    When this function is used, no 'message' event will be emitted and the onmessage listener will not be invoked.