Skip to content

Send a cross process message if message channel is connected.

License

Notifications You must be signed in to change notification settings

node-modules/sendmessage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sendmessage

NPM version CI Test coverage npm download

Send a cross process message if message channel is connected. Avoid channel closed error throw out.

Install

npm install sendmessage --save

Usage

master.js

import { fork } from 'node:child_process';
import sendmessage from 'sendmessage';

const worker = fork('./worker.js');

sendmessage(worker, { hi: 'this is a message to worker' });

worker.js

import sendmessage from 'sendmessage';

sendmessage(process, { hello: 'this is a message to master' });

API

#sendmessage(childProcess, message)

Send a cross process message. If a process is not child process, this will just call process.emit('message', message) instead.

  • childProcess: child process instance
  • message: the message need to send
sendmessage(process, { hello: 'this is a message to master' });

You can switch to process.emit('message', message) using process.env.SENDMESSAGE_ONE_PROCESS

License

MIT

Contributors

Contributors

Made with contributors-img.