Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference b/w event.on() and event.once() in nodejs

I'm testing the plus_one app, and while running it, I just wanted to clarify my concepts on event.once() and event.on()

This is plus_one.js

> process.stdin.resume(); process.stdin.on('data',function(data){     var number;     try{         number=parseInt(data.toString(),10);         number+=1;         process.stdout.write(number+"\n");         }     catch(err){         process.stderr.write(err.message+"\n");         }     }); 

and this is test_plus_one.js

var spawn=require('child_process').spawn; var child=spawn('node',['plus_one.js']);  setInterval(function(){     var number=Math.floor(Math.random()*10000);     child.stdin.write(number+"\n");     child.stdout.on('data',function(data){         console.log('child replied to '+number+' with '+data);         });     },1000); 

I get few maxlistener offset warning while using child.stdin.on() but this is not the case when using child.stdin.once(), why is this happening ?

Is it because child.stdin is listening to previous inputs ? but in that case maxlistener offset should be set more frequently, but its only happening for once or twice in a minute.

like image 994
Anathema.Imbued Avatar asked Sep 11 '13 11:09

Anathema.Imbued


People also ask

What is the difference between on and addListener in NodeJS?

There are no differences between them.

What is once in NodeJS?

Using once() Sometimes you want your application to respond to an event (or type of event) only one time (i.e., the first time the event occurs). To do this, Node provides the once() method. It is used just like the addListener() and on() methods, but allows for responding to the event only once.

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 event and event emitter in NodeJS?

The EventEmitter is a module that facilitates communication/interaction between objects in Node. EventEmitter is at the core of Node asynchronous event-driven architecture. Many of Node's built-in modules inherit from EventEmitter including prominent frameworks like Express.


2 Answers

Using EventEmitter.on(), you attach a full listener, versus when you use EventEmitter.once(), it is a one time listener that will detach after firing once. Listeners that only fire once don't count towards the max listener count.

like image 148
hexacyanide Avatar answered Oct 12 '22 22:10

hexacyanide


According to the latest official docs https://nodejs.org/api/events.html#events_eventemitter_defaultmaxlisteners. The .once() listener does count to the maxlisteners.

emitter.setMaxListeners(emitter.getMaxListeners() + 1); emitter.once('event', () => {   // do stuff   emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0)); }); 
like image 28
Yuefei Ma Avatar answered Oct 12 '22 22:10

Yuefei Ma