{ "source": "doc/api/events.markdown", "modules": [ { "textRaw": "Events", "name": "Events", "stability": 2, "stabilityText": "Stable", "type": "module", "desc": "
Many objects in io.js 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
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
\nFunctions 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
Use require('events')
to access the EventEmitter class.\n\n
var EventEmitter = require('events');
\nWhen an EventEmitter
instance experiences an error, the typical action is\nto emit an 'error'
event. Error events are treated as a special case in\nio.js. If there is no listener for it, then the default action is to print\na stack trace and exit the program.\n\n
All EventEmitters emit the event 'newListener'
when new listeners are\nadded and 'removeListener'
when a listener is removed.\n\n
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
server.on('connection', function (stream) {\n console.log('someone connected!');\n});
\nReturns 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
server.on('connection', function (stream) {\n console.log('someone connected!');\n});
\nReturns 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
\nserver.once('connection', function (stream) {\n console.log('Ah, we have our first user!');\n});
\nReturns 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
\nvar callback = function(stream) {\n console.log('someone connected!');\n};\nserver.on('connection', callback);\n// ...\nserver.removeListener('connection', callback);
\nremoveListener
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
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
\nReturns 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\nmemory leaks. Obviously not all Emitters should be limited to 10. This function\nallows that to be increased. Set to zero for unlimited.\n\n
\nReturns emitter, so calls can be chained.\n\n
\n", "signatures": [ { "params": [ { "name": "n" } ] } ] }, { "textRaw": "emitter.getMaxListeners()", "type": "method", "name": "getMaxListeners", "desc": "Returns the current max listener value for the emitter which is either set by\nemitter.setMaxListeners(n)
or defaults to EventEmitter.defaultMaxListeners
.\n\n
This can be useful to increment/decrement max listeners to avoid the warning\nwhile not being irresponsible and setting a too big number.\n\n
\nemitter.setMaxListeners(emitter.getMaxListeners() + 1);\nemitter.once('event', function () {\n // do stuff\n emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0));\n});
\n",
"signatures": [
{
"params": []
}
]
},
{
"textRaw": "emitter.listeners(event)",
"type": "method",
"name": "listeners",
"desc": "Returns a copy of the array of listeners for the specified event.\n\n
\nserver.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
\nReturns true
if event had listeners, false
otherwise.\n\n\n
Returns the number of listeners listening to the type
of event.\n\n
emitter.setMaxListeners(n)
sets the maximum on a per-instance basis.\nThis class property lets you set it for all EventEmitter
instances,\ncurrent and future, effective immediately. Use with care.\n\n
Note that emitter.setMaxListeners(n)
still has precedence over\nEventEmitter.defaultMaxListeners
.\n\n\n
Returns the number of listeners for a given event.\n\n
\n", "signatures": [ { "params": [ { "name": "emitter" }, { "name": "event" } ] } ] } ], "events": [ { "textRaw": "Event: 'newListener'", "type": "event", "name": "newListener", "params": [], "desc": "This event is emitted before a listener is added. When this event is\ntriggered, the listener has not been added to the array of listeners for the\nevent
. Any listeners added to the event name
in the newListener event\ncallback will be added before the listener that is in the process of being\nadded.\n\n\n
This event is emitted after a listener is removed. When this event is\ntriggered, the listener has been removed from the array of listeners for the\nevent
.\n\n
Inheriting from EventEmitter
is no different from inheriting from any other\nconstructor function. For example:\n\n
'use strict';\nconst util = require('util');\nconst EventEmitter = require('events').EventEmitter;\n\nfunction MyEventEmitter() {\n // Initialize necessary properties from `EventEmitter` in this instance\n EventEmitter.call(this);\n}\n\n// Inherit functions from `EventEmitter`'s prototype\nutil.inherits(MyEventEmitter, EventEmitter);
\n",
"type": "module",
"displayName": "Inheriting from 'EventEmitter'"
}
]
}
]
}
]
}