Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

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'); 
like image 720
Rahul Kumar Avatar asked Apr 25 '15 06:04

Rahul Kumar


People also ask

What is the difference between addListener and addEventListener?

The differrence is that addListener() requires only one parameter - the object that will be the listener. addEventListener also requires a string which is the name of the event that will be triggered. When I am listening for multiple events, this would necessitate multiple listeners - a big pain.

What is addListener in Javascript?

The addEventListener() method allows you to add event listeners on any HTML DOM object such as HTML elements, the HTML document, the window object, or other objects that support events, like the xmlHttpRequest object.

What is event listener in node js?

Node. js uses events module to create and handle custom events. The EventEmitter class can be used to create and handle custom events module.

Which method is used to register an event listener Nodejs?

The eventEmitter. on() method is used to register listeners, while the eventEmitter.


2 Answers

.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).

like image 141
jfriend00 Avatar answered Oct 19 '22 23:10

jfriend00


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."); 
like image 22
user4013241 Avatar answered Oct 20 '22 00:10

user4013241