VOOZH about

URL: https://www.geeksforgeeks.org/node-js/node-js-messagechannel-postmessage-method/

⇱ Node.js MessageChannel.postMessage() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Node.js MessageChannel.postMessage() Method

Last Updated : 1 Feb, 2023

The MessageChannel.postMessage() method is an inbuilt application programming interface of class Worker within worker_threads module which is used to send the message from one port to another.

Syntax:

const MessageChannel.postMessage(value[, transferList])

Parameters: This method takes the value as a parameter which can contain any kind of object.

Return Value: This method sends the message from one port to another.

Example 1: Filename: index.js

Output:

received data in port1 :
{ message: { hello: 'world2' } }
received data in port2 :
{ message: { hello: 'world1' } }

Example 2: Filename: index.js

Output:

GFG
closed!

Run the index.js file using the following command:

node index.js

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/worker_threads.html#worker_threads_port_postmessage_value_transferlist

Comment
Article Tags:

Explore