Alias for emitter.on(eventName, listener).
Alias for emitter.on(eventName, listener).
emitter.addListener(eventName, listener)
on()
When they are disconnected all internal handles will be closed, allowing the master process to die gracefully if no other event is waiting.
When they are disconnected all internal handles will be closed, allowing the master process to die gracefully if no other event is waiting.
The method takes an optional callback argument which will be called when finished.
This can only be called from the master process.
cluster.disconnect([callback])
Synchronously calls each of the listeners registered for the event named eventName, in the order they were registered, passing the supplied arguments to each.
Synchronously calls each of the listeners registered for the event named eventName, in the order they were registered, passing the supplied arguments to each.
Returns true if the event had listeners, false otherwise.
the event name
the event arguments
emitter.emit(name[, arg1][, arg2][, ...])
Spawn a new worker process.
Spawn a new worker process. This can only be called from the master process.
a new worker
cluster.fork([env])
Returns the current max listener value for the EventEmitter which is either set by emitter.setMaxListeners(n) or defaults to EventEmitter.defaultMaxListeners.
Returns the current max listener value for the EventEmitter which is either set by emitter.setMaxListeners(n) or defaults to EventEmitter.defaultMaxListeners.
emitter.getMaxListeners()
setMaxListeners()
True if the process is a master.
True if the process is a master. This is determined by the process.env.NODE_UNIQUE_ID. If process.env.NODE_UNIQUE_ID is undefined, then isMaster is true.
True if the process is not a master (it is the negation of cluster.isMaster).
Returns the number of listeners listening to the event named eventName.
Returns the number of listeners listening to the event named eventName.
emitter.listenerCount(eventName)
Returns a copy of the array of listeners for the event named eventName.
Returns a copy of the array of listeners for the event named eventName.
emitter.listeners(eventName)
Adds the listener function to the end of the listeners array for the event named eventName.
Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.
Returns a reference to the EventEmitter so calls can be chained.
emitter.on(eventName, listener)
Adds a one time listener function for the event named eventName.
Adds a one time listener function for the event named eventName. This listener is invoked only the next time eventName is triggered, after which it is removed.
emitter.once(eventName, listener)
Removes all listeners, or those of the specified eventName.
Removes all listeners, or those of the specified eventName.
Note that it is bad practice to remove listeners added elsewhere in the code, particularly when the EventEmitter instance was created by some other component or module (e.g. sockets or file streams).
Returns a reference to the EventEmitter so calls can be chained.
emitter.removeAllListeners([eventName])
Removes all listeners, or those of the specified eventName.
Removes all listeners, or those of the specified eventName.
Note that it is bad practice to remove listeners added elsewhere in the code, particularly when the EventEmitter instance was created by some other component or module (e.g. sockets or file streams).
Returns a reference to the EventEmitter so calls can be chained.
emitter.removeAllListeners([eventName])
Removes the specified listener from the listener array for the event named eventName.
Removes the specified listener from the listener array for the event named eventName. removeListener will remove, at most, one instance of a listener from the listener array. If any single listener has been added multiple times to the listener array for the specified eventName, then removeListener must be called multiple times to remove each instance.
Note that once an event has been emitted, all listeners attached to it at the time of emitting will be called in order. This implies that any removeListener() or removeAllListeners() calls after emitting and before the last listener finishes execution will not remove them from emit() in progress. Subsequent events will behave as expected.
emitter.removeListener(eventName, listener)
The scheduling policy, either cluster.SCHED_RR for round-robin or cluster.SCHED_NONE to leave it to the operating system.
The scheduling policy, either cluster.SCHED_RR for round-robin or cluster.SCHED_NONE to leave it to the operating system. This is a global setting and effectively frozen once you spawn the first worker or call cluster.setupMaster(), whatever comes first.
SCHED_RR is the default on all operating systems except Windows. Windows will change to SCHED_RR once libuv is able to effectively distribute IOCP handles without incurring a large performance hit.
cluster.schedulingPolicy can also be set through the NODE_CLUSTER_SCHED_POLICY environment variable. Valid values are "rr" and "none".
By default EventEmitters will print a warning if more than 10 listeners are added for a particular event.
By default EventEmitters will print a warning if more than 10 listeners are added for a particular event. This is a useful default that helps finding memory leaks. Obviously, not all events should be limited to just 10 listeners. The emitter.setMaxListeners() method allows the limit to be modified for this specific EventEmitter instance. The value can be set to Infinity (or 0) for to indicate an unlimited number of listeners.
Returns a reference to the EventEmitter so calls can be chained.
emitter.setMaxListeners(n)
After calling .setupMaster() (or .fork()) this settings object will contain the settings, including the default values.
After calling .setupMaster() (or .fork()) this settings object will contain the settings, including the default values. It is effectively frozen after being set, because .setupMaster() can only be called once. This object is not supposed to be changed or set manually, by you.
setupMaster is used to change the default 'fork' behavior.
setupMaster is used to change the default 'fork' behavior. Once called, the settings will be present in cluster.settings. Note that:
cluster.setupMaster([settings])
A reference to the current worker object.
A reference to the current worker object. Not available in the master process.
A hash that stores the active worker objects, keyed by id field.
A hash that stores the active worker objects, keyed by id field. Makes it easy to loop through all the workers. It is only available in the master process.
A worker is removed from cluster.workers after the worker has disconnected and exited. The order between these two events cannot be determined in advance. However, it is guaranteed that the removal from the cluster.workers list happens before last 'disconnect' or 'exit' event is emitted.
A single instance of Node.js runs in a single thread. To take advantage of multi-core systems the user will sometimes want to launch a cluster of Node.js processes to handle the load.
The cluster module allows you to easily create child processes that all share server ports.