Skip to content

Instantly share code, notes, and snippets.

@nethoncho
Created December 15, 2017 14:28
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nethoncho/b1160edacfe3e12a336c34e98f04cb2a to your computer and use it in GitHub Desktop.
Save nethoncho/b1160edacfe3e12a336c34e98f04cb2a to your computer and use it in GitHub Desktop.
pm2 IPC Minimum needed to send message to process
/*
The online documents were not very clear.
http://pm2.keymetrics.io/docs/usage/pm2-api/#send-message-to-process
https://stackoverflow.com/a/35504369/3386260
https://github.com/pm2-hive/pm2-hive.github.io/pull/117
*/
// Sender
// The sender can run inside or outside of pm2
var pm2 = require('pm2');
var neighborIds = [];
pm2.connect(function() {
// Find the IDs of who you want to send to
pm2.list(function(err, processes) {
for (var i in processes) {
console.log('Id:', processes[i].pm_id, 'Name:', processes[i].name)
if(processes[i].name == 'scout') {
neighborIds.push(processes[i].pm_id);
}
}
// Call this once for each neighborIds
pm2.sendDataToProcessId(MyNeighborId, {
data : {
some : 'data',
},
topic: 'some topic'
}, function(err, res) {
console.log(err, res);
});
console.log('Ids to send messages to:', neighborIds);
});
});
// Receiver
// No need to require require('pm2') however the receiver must be running inside of pm2
process.on('message', function(packet) {
console.log(packet);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment