Node.JS Event Emitter

In the previous article we saw how to generate and handle events in node.js. We saw that eventEmitter class of events module was responsible for binding events with event handler as well as firing events. However, node.js event emitter class has far more capabilities than this. It contains many useful functions that can be used to perform a variety of task. In this article we shall see those node.js event emitter functions in detail.

Common Node Event Emitter Functions

Following are some of the most commonly used functions

addListener(event, listener)

The addListener function binds the event passed as first parameter to the listener passed as second parameter. This function can be called multiple times. Each call binds listener at the end of the listener loop. The function returns the eventEmitter object.

removeListener(event, listener)

This event removes the listener specified as the second argument from the list of the listeners corresponding to the event specified as the first argument. It is important to note here that this method only remove one instance of the specified listener. If you have multiple listeners that you want to remove, you should call this method multiple times.

once(event, listener)

This function adds listener to the event and executes only once. When the event is fired for the first time, this listener executes and then it is removed from the list of the listeners.

on(event, listener)

This is similar to the addListener function. It binds the event passed as first parameter to the listener passed as second parameter. This function can be called multiple times. Each call binds listener at the end of the listener loop. The function returns the eventEmitter object.

listeners(event)

It returns array of all listeners bound with the event specified as a parameter.

Take a look at the following example. Here we shall implement some of the functions discusses above.

var events = require('events');
var eE = new events.EventEmitter();


var lis1 = function lis1() {
   console.log('First listener executed.');
}


var lis2 = function lis2() {
  console.log('Second Listener executed.');
}

var lis3 = function lis3() {
  console.log('Third Listener executed.');
}

eE.addListener('conn', lis1);


eE.on('conn', lis2);

eE.addListener('conn', lis3);

var eventListeners = require('events').EventEmitter.listenerCount
   (eE,'conn');
console.log(eventListeners + " listeners attached with connection event");


eE.emit('conn');


eE.removeListener('conn', lis1);
console.log("Lis1 removed from conn event.");


eE.emit('conn');

eventListeners = require('events').EventEmitter.listenerCount(eE,'conn');
console.log(eventListeners + " listeners attached with connection event.");

console.log("Program Execution Complete");

Download the code Try the code

In the above code, three listeners are added to the event loop of “conn” event. Then via eventEmitter object eE, the conn event is fired. This causes the three listeners to execute. Next, the first listeners is removed from the last. The conn event is fired again. However, this time only the second and third listeners shall execute.

Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .