Merge remote-tracking branch 'origin/v0.10'
[platform/upstream/nodejs.git] / doc / api / events.markdown
1 # Events
2
3     Stability: 4 - API Frozen
4
5 <!--type=module-->
6
7 Many objects in Node emit events: a `net.Server` emits an event each time
8 a peer connects to it, a `fs.readStream` emits an event when the file is
9 opened. All objects which emit events are instances of `events.EventEmitter`.
10 You can access this module by doing: `require("events");`
11
12 Typically, event names are represented by a camel-cased string, however,
13 there aren't any strict restrictions on that, as any string will be accepted.
14
15 Functions can then be attached to objects, to be executed when an event
16 is emitted. These functions are called _listeners_. Inside a listener
17 function, `this` refers to the `EventEmitter` that the listener was
18 attached to.
19
20
21 ## Class: events.EventEmitter
22
23 To access the EventEmitter class, `require('events').EventEmitter`.
24
25 When an `EventEmitter` instance experiences an error, the typical action is
26 to emit an `'error'` event.  Error events are treated as a special case in node.
27 If there is no listener for it, then the default action is to print a stack
28 trace and exit the program.
29
30 All EventEmitters emit the event `'newListener'` when new listeners are
31 added and `'removeListener'` when a listener is removed.
32
33 ### emitter.addListener(event, listener)
34 ### emitter.on(event, listener)
35
36 Adds a listener to the end of the listeners array for the specified event.
37
38     server.on('connection', function (stream) {
39       console.log('someone connected!');
40     });
41
42 ### emitter.once(event, listener)
43
44 Adds a **one time** listener for the event. This listener is
45 invoked only the next time the event is fired, after which
46 it is removed.
47
48     server.once('connection', function (stream) {
49       console.log('Ah, we have our first user!');
50     });
51
52 ### emitter.removeListener(event, listener)
53
54 Remove a listener from the listener array for the specified event.
55 **Caution**: changes array indices in the listener array behind the listener.
56
57     var callback = function(stream) {
58       console.log('someone connected!');
59     };
60     server.on('connection', callback);
61     // ...
62     server.removeListener('connection', callback);
63
64
65 ### emitter.removeAllListeners([event])
66
67 Removes all listeners, or those of the specified event.
68
69
70 ### emitter.setMaxListeners(n)
71
72 By default EventEmitters will print a warning if more than 10 listeners are
73 added for a particular event. This is a useful default which helps finding
74 memory leaks. Obviously not all Emitters should be limited to 10. This function
75 allows that to be increased. Set to zero for unlimited.
76
77
78 ### EventEmitter.defaultMaxListeners
79
80 `emitter.setMaxListeners(n)` sets the maximum on a per-instance basis.
81 This class property lets you set it for *all* `EventEmitter` instances,
82 current and future, effective immediately. Use with care.
83
84 Note that `emitter.setMaxListeners(n)` still has precedence over
85 `EventEmitter.defaultMaxListeners`.
86
87
88 ### emitter.listeners(event)
89
90 Returns an array of listeners for the specified event.
91
92     server.on('connection', function (stream) {
93       console.log('someone connected!');
94     });
95     console.log(util.inspect(server.listeners('connection'))); // [ [Function] ]
96
97
98 ### emitter.emit(event, [arg1], [arg2], [...])
99
100 Execute each of the listeners in order with the supplied arguments.
101
102
103 ### Class Method: EventEmitter.listenerCount(emitter, event)
104
105 Return the number of listeners for a given event.
106
107
108 ### Event: 'newListener'
109
110 * `event` {String} The event name
111 * `listener` {Function} The event handler function
112
113 This event is emitted any time someone adds a new listener.
114
115
116 ### Event: 'removeListener'
117
118 * `event` {String} The event name
119 * `listener` {Function} The event handler function
120
121 This event is emitted any time someone removes a listener.