Skip to content

Instantly share code, notes, and snippets.

@jhliberty
Forked from gregberge/cluster.js
Last active August 29, 2015 14:17
Show Gist options
  • Save jhliberty/52be6bfa45e7d66433b9 to your computer and use it in GitHub Desktop.
Save jhliberty/52be6bfa45e7d66433b9 to your computer and use it in GitHub Desktop.
var cluster = require('cluster');
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < 20; i++) {
cluster.fork();
}
return ;
}
//
// Starting this line, we are in a worker mode.
//
var http = require('http');
var Primus = require('primus');
var PrimusCluster = require('primus-cluster');
var server = http.createServer().listen(8000);
var primus = new Primus(server);
primus.use('cluster', PrimusCluster);
server.on('request', function handler(req, res) {
// Connect to primus and show an alert if a "hello" message is received.
if (req.url === '/') {
res.end('\
<script src="/primus/primus.js"></script>\
<script>\
var primus = new Primus();\
primus.on("data", function (data) {\
if (data === "hello") alert("hello");\
});\
</script>\
');
}
// Broadcast an "hello" message.
if (req.url === '/broadcast') {
primus.write('hello');
res.end('broadcasted');
}
res.statusCode = 404;
res.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment