Skip to content

Instantly share code, notes, and snippets.

@ndelangen
Created January 2, 2017 16:18
Show Gist options
  • Save ndelangen/c8863e353ba1558b2c0c62239af9ea82 to your computer and use it in GitHub Desktop.
Save ndelangen/c8863e353ba1558b2c0c62239af9ea82 to your computer and use it in GitHub Desktop.
A NodeJS cluster communicating via ipc.
const cluster = require('cluster');
const http = require('http');
if (cluster.isMaster) {
// init cluster
require('os').cpus().forEach(() => {
cluster.fork();
});
// add eventlisteners
Object.values(cluster.workers).forEach(worker => {
worker.on('message', message => {
console.log(message);
});
});
} else {
http.Server((req, res) => {
res.send(200, 'hello world\n');
process.send('Hi');
}).listen(8000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment