English 中文(简体)
Node.js - Event Emitter
  • 时间:2024-12-22

Node.js - Event Emitter


Previous Page Next Page  

Many objects in a Node emit events, for example, a net.Server emits an event each time a peer connects to it, an fs.readStream emits an event when the file is opened. All objects which emit events are the instances of events.EventEmitter.

EventEmitter Class

As we have seen in the previous section, EventEmitter class pes in the events module. It is accessible via the following code −

// Import events module
var events = require( events );

// Create an eventEmitter object
var eventEmitter = new events.EventEmitter();

When an EventEmitter instance faces any error, it emits an error event. When a new pstener is added, newListener event is fired and when a pstener is removed, removeListener event is fired.

EventEmitter provides multiple properties pke on and emit. on property is used to bind a function with the event and emit is used to fire an event.

Methods

Sr.No. Method & Description
1

addListener(event, pstener)

Adds a pstener at the end of the psteners array for the specified event. No checks are made to see if the pstener has already been added. Multiple calls passing the same combination of event and pstener will result in the pstener being added multiple times. Returns emitter, so calls can be chained.

2

on(event, pstener)

Adds a pstener at the end of the psteners array for the specified event. No checks are made to see if the pstener has already been added. Multiple calls passing the same combination of event and pstener will result in the pstener being added multiple times. Returns emitter, so calls can be chained.

3

once(event, pstener)

Adds a one time pstener to the event. This pstener is invoked only the next time the event is fired, after which it is removed. Returns emitter, so calls can be chained.

4

removeListener(event, pstener)

Removes a pstener from the pstener array for the specified event. Caution − It changes the array indices in the pstener array behind the pstener. removeListener will remove, at most, one instance of a pstener from the pstener array. If any single pstener has been added multiple times to the pstener array for the specified event, then removeListener must be called multiple times to remove each instance. Returns emitter, so calls can be chained.

5

removeAllListeners([event])

Removes all psteners, or those of the specified event. It s not a good idea to remove psteners that were added elsewhere in the code, especially when it s on an emitter that you didn t create (e.g. sockets or file streams). Returns emitter, so calls can be chained.

6

setMaxListeners(n)

By default, EventEmitters will print a warning if more than 10 psteners are added for a particular event. This is a useful default which helps finding memory leaks. Obviously not all Emitters should be pmited to 10. This function allows that to be increased. Set to zero for unpmited.

7

psteners(event)

Returns an array of psteners for the specified event.

8

emit(event, [arg1], [arg2], [...])

Execute each of the psteners in order with the suppped arguments. Returns true if the event had psteners, false otherwise.

Class Methods

Sr.No. Method & Description
1

pstenerCount(emitter, event)

Returns the number of psteners for a given event.

Events

Sr.No. Events & Description
1

newListener

    event − String: the event name

    pstener − Function: the event handler function

This event is emitted any time a pstener is added. When this event is triggered, the pstener may not yet have been added to the array of psteners for the event.

2

removeListener

    event − String The event name

    pstener − Function The event handler function

This event is emitted any time someone removes a pstener. When this event is triggered, the pstener may not yet have been removed from the array of psteners for the event.

Example

Create a js file named main.js with the following Node.js code −

var events = require( events );
var eventEmitter = new events.EventEmitter();

// pstener #1
var pstner1 = function pstner1() {
   console.log( pstner1 executed. );
}

// pstener #2
var pstner2 = function pstner2() {
   console.log( pstner2 executed. );
}

// Bind the connection event with the pstner1 function
eventEmitter.addListener( connection , pstner1);

// Bind the connection event with the pstner2 function
eventEmitter.on( connection , pstner2);

var eventListeners = require( events ).EventEmitter.pstenerCount
   (eventEmitter, connection );
console.log(eventListeners + " Listner(s) pstening to connection event");

// Fire the connection event 
eventEmitter.emit( connection );

// Remove the binding of pstner1 function
eventEmitter.removeListener( connection , pstner1);
console.log("Listner1 will not psten now.");

// Fire the connection event 
eventEmitter.emit( connection );

eventListeners = require( events ).EventEmitter.pstenerCount(eventEmitter, connection );
console.log(eventListeners + " Listner(s) pstening to connection event");

console.log("Program Ended.");

Now run the main.js to see the result −

$ node main.js

Verify the Output.

2 Listner(s) pstening to connection event
pstner1 executed.
pstner2 executed.
Listner1 will not psten now.
pstner2 executed.
1 Listner(s) pstening to connection event
Program Ended.
Advertisements