What is the difference between addListener(event, listener) and on(event, listener) method in node.js?

node.js

node.js Problem Overview


Here i cannot understand what is the basic difference between these two methods.

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



var listner1 = function listner1() {
    console.log('listner1 executed.');
}
    
var listner2 = function listner2() {
    console.log('listner2 executed.');    
}

eventEmitter.addListener('connection', listner1);
    
eventEmitter.on('connection', listner2);
    
eventEmitter.emit('connection');

node.js Solutions


Solution 1 - node.js

.on() is exactly the same as .addListener() in the EventEmitter object.

Straight from the EventEmitter source code:

EventEmitter.prototype.on = EventEmitter.prototype.addListener;

Sleuthing through the GitHub repository, there is this checkin from Jul 3, 2010 that contains the comment: "Experimental: 'on' as alias to 'addListener'".


Update in 2017: The documentation for EventEmitter.prototype.addListener() now says this:

> Alias for emitter.on(eventName, listener).

Solution 2 - node.js

Yes you can use "removeListener" with with a listener created with "on". Try it.

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

// listener #1
var listner1 = function listner1() {
   console.log('listner1 executed.');
}

// listener #2
var listner2 = function listner2() {
  console.log('listner2 executed.');
}

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

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

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

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

// Remove the binding of listner1 function
eventEmitter.removeListener('connection', listner2);
console.log("Listner2 will not listen now.");

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

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

console.log("Program Ended.");

Solution 3 - node.js

Added in: v10.0.0

emitter.off(eventName, listener)

Alias for emitter.removeListener()

Solution 4 - node.js

Their functionalities are exactly the same, however, they can be used in different ways to make your code efficient. Lets assume you created a server and you create a listener, by using ".addListener(event, listener)", for every user that connects to your server. Now as soon as a user is disconnected, you can remove that listener by using the command "removeListener", but you cannot remove the ".on(event, listener)" command. So, you can use these two commands for different situations.

Solution 5 - node.js

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRahul KumarView Question on Stackoverflow
Solution 1 - node.jsjfriend00View Answer on Stackoverflow
Solution 2 - node.jsuser4013241View Answer on Stackoverflow
Solution 3 - node.jsuser10684840View Answer on Stackoverflow
Solution 4 - node.jsShahrouzView Answer on Stackoverflow
Solution 5 - node.jsLi ZhengView Answer on Stackoverflow