API - SocketCluster (Server)
The SocketCluster object represents an entire SocketCluster instance (master process which manages a number of child processes). This object's constructor accepts an 'options' object which allows you to configure your SC instance.
Note that since SC v2.2.38, the options object can be accessed from any backend process. E.g: socketCluster.options (in master process - server.js), worker.options (in workerController) and broker.options (in brokerController).
You can also add custom properties to the options object if you want to pass data to various processes. Just make sure that whatever custom data you provide can be serialized to JSON (so you can't pass functions around - However, you can provide a module name as a string and require() the module from inside the target process).
Inherits from:
Properties:
options | Internal options object which defines the behaviour of SC. |
Events:
'fail' | Any error from any child process or master will cause the 'fail' event to be emitted on your SocketCluster instance (assuming the propagateErrors option is not set to false). |
'warning' | Triggered by a warning from any child process or master. |
'ready' | Happens when SocketCluster has booted up and is ready to accept connections. |
'workerStart' | Emitted whenever a worker is launched. This event's handler can take a workerInfo object as argument. This workerInfo object has an id property (the lead worker will always have id 0), a pid property and a respawn property which indicates whether or not the worker respawned (not the first launch). |
'workerExit' | Emitted whenever a worker exits. This event's handler can take a workerInfo object as argument. This workerInfo object has an id property (the id of the worker), a pid property, a code property (the exit code) and a signal property (if terminated using a signal). |
'workerMessage' | Emitted when a worker process sends a message to this master process. The first parameter passed to the handler is the worker id, the second parameter is the data/object sent by the worker, the third parameter is the respond callback. See sendToMaster() method in SCWorker API for details on how to send a message to master from inside a worker process. |
'workerClusterStart' | Emitted whenever the WorkerCluster is launched (the WorkerCluster handles load balancing between workers). This event's handler can take a workerClusterInfo object as argument. This workerClusterInfo object has a pid property and a childProcess property which is a reference to the WorkerCluster process. |
'workerClusterReady' | Emitted whenever the WorkerCluster is ready (after all of its child workers have launched). This event's handler can take a workerClusterInfo object as argument. This workerClusterInfo object has a pid property and a childProcess property which is a reference to the WorkerCluster process. |
'workerClusterExit' | Emitted whenever the WorkerCluster exits. This event's handler can take a workerClusterInfo object as argument. This workerClusterInfo object has a pid property, a code property (the exit code) and a signal property (if terminated using a signal) and a childProcess property which is a reference to the WorkerCluster process. |
'brokerStart' | Emitted whenever a broker is launched. This event's handler can take a brokerInfo object as argument. This brokerInfo object has an id property (the lead broker will always have id 0), a pid property and a respawn property which indicates whether or not the broker respawned (not the first launch). |
'brokerExit' | Emitted whenever a broker exits. This event's handler can take a brokerInfo object as argument. This brokerInfo object has an id property (the id of the broker), a pid property, a code property (the exit code) and a signal property (if terminated using a signal). |
'brokerMessage' | Emitted when a broker process sends a message to this master process. The first parameter passed to the handler is the broker id, the second parameter is the data/object sent by the broker, the third parameter is the respond callback. See sendToMaster() method in Broker API for details on how to send a message to master from inside a broker process. |
Methods:
constructor(options) |
Creates and launches a SocketCluster instance (with config options).
The options object can have any of the following properties (sample showing defaults):
|
sendToWorker(workerId, data, [callback]) |
Send some data to a specific worker process from master.
You can handle this data from inside the worker process by listening for the
'masterMessage' event. See here for more details.
Since SocketCluster v6.6.0, you can provide an optional callback as the third argument; it should be in the
form function (err, data) { /* ... */ } - Note that the worker will need to respond
to the 'masterMessage' event by invoking a respond function; this is an easy way to collect data back from a worker process.
If the worker does not invoke the respond callback, then this callback will receive an instance of TimeoutError as the first argument.
The timeout is defined by the ipcAckTimeout option - See the start options above for details.
|
sendToBroker(brokerId, data, [callback]) |
Send some data to a specific broker process from master.
You can handle this data from inside the broker process by listening for the
'masterMessage' event. See here for more details.
Since SocketCluster v6.6.0, you can provide an optional callback as the third argument; it should be in the
form |
killWorkers() | Kills all worker processes managed by SocketCluster. They will respawn by default. |
killBrokers() | Kills all broker processes managed by SocketCluster. They will respawn by default. |
log(message, [time]) | Log a message internally, time is optional. You generally don't need to call this - It's mostly used internally by SC. |