{ "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.\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.\n\n

\n
server.on('connection', function (stream) {\n  console.log('someone connected!');\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.\n\n

\n
server.on('connection', function (stream) {\n  console.log('someone connected!');\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", "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", "signatures": [ { "params": [ { "name": "event" }, { "name": "listener" } ] } ] }, { "textRaw": "emitter.removeAllListeners([event])", "type": "method", "name": "removeAllListeners", "desc": "

Removes all listeners, or those of the specified event.\n\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", "signatures": [ { "params": [ { "name": "event" }, { "name": "arg1", "optional": true }, { "name": "arg2", "optional": true }, { "name": "...", "optional": true } ] } ] } ], "events": [ { "textRaw": "Event: 'newListener'", "type": "event", "name": "newListener", "params": [], "desc": "

This event is emitted any time someone adds a new listener.\n

\n" } ] } ] } ] }