Skip to content

example of nodejs child_process spawning child process spawning with IPS and standard io (stdio)

Notifications You must be signed in to change notification settings

AbdulxaqDev/spawning-node-child-process

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Example of node child_process spawning with:

  • Stdio (Standard input, output and error)

main.js

const { spawn } = require("child_process");

const childProcess = spawn("node", [`${__dirname}/child.js`], { stdio: ["pipe", "pipe", "pipe"] });

childProcess.stdout.on("data", (message) => {
  console.log(message.toString());
});

childProcess.stdin.write("main-message-stdio");

child.js

process.stdin.on("data", (message) => {
  console.log(message.toString());
  process.exit(0)
});

process.stdout.write("child-message-stdio");

  • IPC (Inter Process Communication)

main.js

const { spawn } = require("child_process");

const childProcess = spawn("node", [`${__dirname}/child.js`], { stdio: ["inherit","inherit","inherit", "ipc"] });

childProcess.on("message", (message) => {
  console.log("[Main] received:", message);
});

childProcess.send("main-message-ipc");

child.js

process.on("message", (message) => {
  console.log("[Child] received:", message);
  process.exit(0)
});

process.send("child-message-ipc");

About

example of nodejs child_process spawning child process spawning with IPS and standard io (stdio)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published