{ "source": "doc/api/events.markdown", "modules": [ { "textRaw": "Events", "name": "Events", "stability": 4, "stabilityText": "API Frozen", "type": "module", "desc": "

Many objects in Node emit events: a net.Server emits an event each time\na peer connects to it, a fs.readStream emits an event when the file is\nopened. All objects which emit events are instances of events.EventEmitter.\nYou can access this module by doing: require("events");\n\n

\n

Typically, event names are represented by a camel-cased string, however,\nthere aren't any strict restrictions on that, as any string will be accepted.\n\n

\n

Functions can then be attached to objects, to be executed when an event\nis emitted. These functions are called listeners. Inside a listener\nfunction, this refers to the EventEmitter that the listener was\nattached to.\n\n\n

\n", "classes": [ { "textRaw": "Class: events.EventEmitter", "type": "class", "name": "events.EventEmitter", "desc": "

To access the EventEmitter class, require('events').EventEmitter.\n\n

\n

When an EventEmitter instance experiences an error, the typical action is\nto emit an 'error' event. Error events are treated as a special case in node.\nIf there is no listener for it, then the default action is to print a stack\ntrace and exit the program.\n\n

\n

All EventEmitters emit the event 'newListener' when new listeners are\nadded and 'removeListener' when a listener is removed.\n\n

\n", "methods": [ { "textRaw": "emitter.addListener(event, listener)", "type": "method", "name": "addListener", "desc": "

Adds a listener to the end of the listeners array for the specified event.\nNo checks are made to see if the listener has already been added. Multiple\ncalls passing the same combination of event and listener will result in the\nlistener being added multiple times.\n\n

\n
server.on('connection', function (stream) {\n  console.log('someone connected!');\n});
\n

Returns emitter, so calls can be chained.\n\n

\n", "signatures": [ { "params": [ { "name": "event" }, { "name": "listener" } ] }, { "params": [ { "name": "event" }, { "name": "listener" } ] } ] }, { "textRaw": "emitter.on(event, listener)", "type": "method", "name": "on", "desc": "

Adds a listener to the end of the listeners array for the specified event.\nNo checks are made to see if the listener has already been added. Multiple\ncalls passing the same combination of event and listener will result in the\nlistener being added multiple times.\n\n

\n
server.on('connection', function (stream) {\n  console.log('someone connected!');\n});
\n

Returns emitter, so calls can be chained.\n\n

\n", "signatures": [ { "params": [ { "name": "event" }, { "name": "listener" } ] } ] }, { "textRaw": "emitter.once(event, listener)", "type": "method", "name": "once", "desc": "

Adds a one time listener for the event. This listener is\ninvoked only the next time the event is fired, after which\nit is removed.\n\n

\n
server.once('connection', function (stream) {\n  console.log('Ah, we have our first user!');\n});
\n

Returns emitter, so calls can be chained.\n\n

\n", "signatures": [ { "params": [ { "name": "event" }, { "name": "listener" } ] } ] }, { "textRaw": "emitter.removeListener(event, listener)", "type": "method", "name": "removeListener", "desc": "

Remove a listener from the listener array for the specified event.\nCaution: changes array indices in the listener array behind the listener.\n\n

\n
var callback = function(stream) {\n  console.log('someone connected!');\n};\nserver.on('connection', callback);\n// ...\nserver.removeListener('connection', callback);
\n

removeListener will remove, at most, one instance of a listener from the\nlistener array. If any single listener has been added multiple times to the\nlistener array for the specified event, then removeListener must be called\nmultiple times to remove each instance.\n\n

\n

Returns emitter, so calls can be chained.\n\n

\n", "signatures": [ { "params": [ { "name": "event" }, { "name": "listener" } ] } ] }, { "textRaw": "emitter.removeAllListeners([event])", "type": "method", "name": "removeAllListeners", "desc": "

Removes all listeners, or those of the specified event. It's not a good idea to\nremove listeners that were added elsewhere in the code, especially when it's on\nan emitter that you didn't create (e.g. sockets or file streams).\n\n

\n

Returns emitter, so calls can be chained.\n\n

\n", "signatures": [ { "params": [ { "name": "event", "optional": true } ] } ] }, { "textRaw": "emitter.setMaxListeners(n)", "type": "method", "name": "setMaxListeners", "desc": "

By default EventEmitters will print a warning if more than 10 listeners are\nadded for a particular event. This is a useful default which helps finding memory leaks.\nObviously not all Emitters should be limited to 10. This function allows\nthat to be increased. Set to zero for unlimited.\n\n\n

\n", "signatures": [ { "params": [ { "name": "n" } ] } ] }, { "textRaw": "emitter.listeners(event)", "type": "method", "name": "listeners", "desc": "

Returns an array of listeners for the specified event.\n\n

\n
server.on('connection', function (stream) {\n  console.log('someone connected!');\n});\nconsole.log(util.inspect(server.listeners('connection'))); // [ [Function] ]
\n", "signatures": [ { "params": [ { "name": "event" } ] } ] }, { "textRaw": "emitter.emit(event, [arg1], [arg2], [...])", "type": "method", "name": "emit", "desc": "

Execute each of the listeners in order with the supplied arguments.\n\n

\n

Returns true if event had listeners, false otherwise.\n\n\n

\n", "signatures": [ { "params": [ { "name": "event" }, { "name": "arg1", "optional": true }, { "name": "arg2", "optional": true }, { "name": "...", "optional": true } ] } ] } ], "classMethods": [ { "textRaw": "Class Method: EventEmitter.listenerCount(emitter, event)", "type": "classMethod", "name": "listenerCount", "desc": "

Return the number of listeners for a given event.\n\n\n

\n", "signatures": [ { "params": [ { "name": "emitter" }, { "name": "event" } ] } ] } ], "events": [ { "textRaw": "Event: 'newListener'", "type": "event", "name": "newListener", "params": [], "desc": "

This event is emitted any time a listener is added. When this event is triggered,\nthe listener may not yet have been added to the array of listeners for the event.\n\n\n

\n" }, { "textRaw": "Event: 'removeListener'", "type": "event", "name": "removeListener", "params": [], "desc": "

This event is emitted any time someone removes a listener. When this event is triggered,\nthe listener may not yet have been removed from the array of listeners for the event.\n

\n" } ] } ] } ] }