Node.js event emitter explained

Node.js event emitter explained

Node.js core API is based on the asynchronous event-driven architecture in which certain kind of objects called emitters periodically emits events that cause listener objects to be called.

All objects that emit events are members of EventEmitter class. These objects expose an _eventEmitter.on()_ function that allows one or more functions to be attached to named events emitted by the object.

When the EventEmitter object emits an event, all of the functions attached to that specific event are called synchronously. All values returned by the called listeners are ignored and will be discarded.

Consider the following example:

const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
myEmitter.on('event', function(a, b) {
  console.log(a, b, this);
  // Prints:
  //   ... MyEmitter {
  //     domain: null,
  //     _events: { event: [Function] },
  //     _eventsCount: 1,
  //     _maxListeners: undefined }
});
myEmitter.emit('event','Technoetics', 'Club');

Here we create a myEmitter object and emit event at the end which triggers the callback function and we are able to get the desired output.

By default, all listeners attached to a particular event object are called by the EventListener object synchronously in the order in which they are registered or attached to the event object.

If we want to break that flow and switch to asynchronous mode then we can use setImmediate() or process.nextTick() methods:

const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
myEmitter.on('event', function(a, b) {
  setImmediate(() => {
    console.log('this happens asynchronously');
  });
  function cb(){
    console.log('processed in next iteration',a,b);
  }
  process.nextTick(cb)
  console.log('processed in first iteration',a,b);
});
myEmitter.emit('event', 'Technoetics', 'Club');
/**
 * Output of the above Program:
 * processed in first iteration 
 * processed in next iteration 
 * this happens asynchronously
 */

As we can see from the output of the above program, the nextTick is called in the iteration after the task of displaying 1st log statement is executed and then in the next cycle of picking up event from event loop task defined in process.nextTick() is called while setImmediate allows running some task asynchronously.

Handling event calls in Node.js:

The listener can be invoked only once using eventEmitter.once() method while every time the event is emitted using eventEmitter.on() method:

const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
const myEmitter2 = new MyEmitter();</span><span id="2c03" class="dp jv jw ff js b gg kb kc kd ke kf jy s jz">let m = 0,n=0;
myEmitter.on('event',() => {
  console.log("M value:",++m);
})
myEmitter.emit('emit');
myEmitter.emit('event');
myEmitter2.once('event', () => {
  console.log("N value:",++n);
});
myEmitter2.emit('event');
// Prints: 1
myEmitter2.emit('event');
//Ignored

This concludes today’s short tutorial on Node.js much of which is inspired by the official docs on node.js event emitter.
You can follow me on LinkedIn or Twitter to keep in touch.
Till then keep coding and learn new stuff 😀

Did you find this article valuable?

Support saurabhnative by becoming a sponsor. Any amount is appreciated!