Created
January 8, 2023 03:08
-
-
Save BSN4/fb0e298dd0fe9271b0a72243ffd8661c to your computer and use it in GitHub Desktop.
pm2 IPC Minimum needed to send message to process
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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