Unitech/pm2 — pull request #214 by bpostlethwaite (2013-12-10 19:45:04+00:00)
Title: add God method for sending message to process

Add a method to God which will allow messaging to process from external RPC calls. This is useful for adding generic code monitoring into the PM2 system (number of connected clients, etc.) or using PM2 to control code flow more directly. This is then available to external tools such as [pm2-interface](https://github.com/Unitech/pm2-interface).

Example:
in your process script:
```javascript
process.on("message", function (msg) {
if ( "type" in msg && msg.type === "god:heap" ) {
var heap = process.memoryUsage().heapUsed
process.send({type:"process:heap", heap:heap})
}
})
```

Then in some monitoring process (using pm2-interface in this case)
```javascript
ipm2.bus.on('process:*', function(event, data) {

if (event === "heap" && "heap" in data) {
console.log("process heap:", data)
}
...
}

var msg = {type:"god:heap", data:x}
ipm2.rpc.msgProcess({name:"worker", msg:msg}, function (err, res) {
if (err) console.log(err)
else console.log(res)
})
```

returns
```shell
process heap: { type: 'process:heap', heap: 7000592 }
```