Skip to content

Instantly share code, notes, and snippets.

@gregberge
Created March 9, 2014 10:32
Show Gist options
  • Save gregberge/9445772 to your computer and use it in GitHub Desktop.
Save gregberge/9445772 to your computer and use it in GitHub Desktop.
Primus cluster
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