Fourk allows you to spawn your web app on multiple threads. Similar to cluster. Fourk works by taking the script it's executed from & re-spawning it as a web worker.
var cluster = require("fourk")();
if (cluster.isMaster) {
// just call fork and you're good to go
for (var i = 4; i--;) cluster.fork()
} else {
// do stuff as worker
}
true if the current script is master
var cluster = require("fourk")();
if (cluster.isMaster) {
// do master stuff
}
true if the current script is a worker
var cluster = require("fourk")();
if (cluster.isWorker) {
// do worker stuff
}
Creates a new child process. Can only be called from master.
var cluster = require("fourk")();
if (cluster.isMaster) {
var worker = cluster.fork();
}
terminates the worker
Broadcasts a message to all workers. Can be called from master or workers.
On master:
var cluster = require("fourk")();
if (cluster.isMaster) {
for (var i = 4; i--;) cluster.fork();
cluster.broadcast("hello", "bob");
} else {
cluster.on("hello", function(name) {
console.log("hello %s!", name); // hello bob! x 4
});
}
on workers:
var cluster = require("fourk")();
if (cluster.isMaster) {
for (var i = 4; i--;) cluster.fork();
} else {
cluster.on("hello", function(name) {
console.log("hello %s!", name); // hello bob! x 4
});
cluster.broadcast("hello", "bob");
}
Broadcasts a message to a worker - round-robin style. If a worker emits a message, it gets sent to master.
Master example:
var cluster = require("fourk")();
if (cluster.isMaster) {
for (var i = 4; i--;) cluster.fork();
cluster.emit("hello", "bob"); // first worker
cluster.emit("hello", "bob"); // second worker
cluster.emit("hello", "bob"); // third worker
} else {
cluster.on("hello", function(name) {
console.log("hello %s!", name); // hello bob! x 3
});
}
Worker example:
var cluster = require("fourk")();
if (cluster.isMaster) {
cluster.fork();
cluster.on("hello", function(name) {
console.log("hello %s!", name); // hello bob! x 1
});
} else {
cluster.emit("hello", "bob");
}
listens for events passed around between workers.
listens for one worker, then disposes it.