Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
Node.js Process warning Event
Next article icon

Node.js Process message Event

Last Updated : 07 Aug, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

The 'message' is an event of class Process within the process module which is emitted whenever a message sent by a parent process using childprocess.send() is received by the child process.

Syntax:

Event: 'message'

Parameters: This event does not accept any argument as a parameter.

Return Value: This event returns nothing but a callback function for further operation.  

Example 1:

The filename is index.js

JavaScript
// Node.js program to demonstrate the   // Process 'message' Event  // Importing process module const cp = require('child_process');  // Initiating child process const process = cp.fork(`${__dirname}/sub.js`);  // Causes the child to print:  // CHILD got message: { hello: 'world' } process.send({ hello: 'world' }); 

Here the file name is sub.js

JavaScript
// Importing process module const process = require('process');  // Message Event process.on('message', (m) => {     console.log('CHILD got message:', m);     process.exit(0) }); 

 
Run the index.js file using the following command:

node index.js

Output:

CHILD got message: { hello: 'world' }

Example 2:

The filename is index.js

JavaScript
// Node.js program to demonstrate the   // Process 'message' Event  // Importing process module const cp = require('child_process');  // Initiating child process const process = cp.fork(`${__dirname}/sub.js`);  // Message Event process.on('message', (m) => {     console.log('PARENT got message:', m); });  // Causes the child to print:  // CHILD got message: { hello: 'world' } process.send({ hello: 'world' }); 

Here the filename is sub.js

JavaScript
// Importing process module const process = require('process');  // Message Event process.on('message', (m) => {     console.log('CHILD got message:', m);     process.exit(0) });  // Causes the parent to print:  // PARENT got message: { foo: 'bar', baz: null } process.send({ foo: 'bar', baz: NaN }); 

Run the index.js file using the following command:

node index.js

Output:

CHILD got message: { hello: 'world' } PARENT got message: { foo: 'bar', baz: null }

Reference: https://nodejs.org/dist/latest-v16.x/docs/api/process.html#process_event_message
 


Next Article
Node.js Process warning Event

R

RohitPrasad3
Improve
Article Tags :
  • Web Technologies
  • Node.js
  • Node.js-process-module

Similar Reads

  • Node.js Process exit Event
    The process is the global object in Node.js that keeps track of and contains all the information of the particular node.js process that is executing at a particular time on the machine.  The process.exit() method is the method that is used to end the Node.js process. Every process action on the mach
    2 min read
  • Node.js process.exit() Method
    The process.exit() method is used to end the process which is running at the same time with an exit code in NodeJS. Syntax: process.exit( code ) Parameter:  This function accepts single parameter as mentioned above and described below: Code: It can be either 0 or 1. 0 means end the process without a
    2 min read
  • Node.js Process Signal Events
    Signals are a POSIX (The Portable Operating System Interface) intercommunication system. These events will be emitted when Node receives signal events. In order to notify that an event has occurred, a notification is sent. The signal handler will receive the signal's name and the signal name is uppe
    3 min read
  • Node.js Process warning Event
    The 'warning' is an event of class Process within the process module which is emitted whenever Node.js emits a process warning. Syntax: Event: 'warning'Parameters: This event does not accept any argument as a parameter. Return Value: This event returns nothing but a callback function for further ope
    2 min read
  • Node.js process.getgid() Method
    The process.getgid() method is an inbuilt application programming interface of the process module which is used to get the numerical group identity of the Node.js process. Syntax: process.getgid() Parameters: This method does not accept any parameters. Return Value: It returns an object specifying t
    1 min read
  • Node.js process.geteuid() Method
    The process.geteuid() method is an inbuilt application programming interface of the process module which is used to get the numerical effective user identity of the Node.js process. Syntax: process.geteuid() Parameters: This method does not accept any parameters. Return Value: This method returns an
    2 min read
  • Node.js process.abort() Method
    The process.abort() property is an inbuilt application programming interface of the process module which is used to abort a running NodeJS process immediately. It also generates a core file. Syntax: process.abort() Parameter: This function does not accept any parameter. Return Type: It has a void re
    2 min read
  • Node.js process.memoryUsage() Method
    The process.memoryUsage() method is an inbuilt method of the process module that provides information about the current processes or runtime of a Node.js program. The memory usage method returns an object describing the memory usage in bytes of the Node.js process. Syntax:process.memoryUsage() Param
    2 min read
  • Node.js Process beforeExit Event
    The 'beforeExit' is an event of class Process within the process module which is emitted when Node.js empties its event loop and has no additional work to schedule. Syntax: Event: 'beforeExit'Parameters: This event does not accept any argument as the parameter. Return Value: This event returns nothi
    2 min read
  • Node.js Process disconnect Event
    In this article, we will discuss about the disconnect event of the Process object. The disconnect event will be issued when the IPC channel is closed if the Node.js process was started using an IPC channel. Syntax: process.on('disconnect', () => { // Disconnect event }); The below examples illust
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences