Created
January 2, 2017 16:18
-
-
Save ndelangen/c8863e353ba1558b2c0c62239af9ea82 to your computer and use it in GitHub Desktop.
A NodeJS cluster communicating via ipc.
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
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