Node.js event emitter explained

Image source: Pixabay

Node.js core API is based on asynchronous event-driven architecture in which certain kind of objects called emitters periodically emit 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:
  //   Technoetics Club 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 Technoetics Club
 * processed in next iteration Technoetics Club
 * 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();
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. You can follow my facebook page: Codeclassifiers to get notified about similar such posts in future. Till then keep coding and learn new stuff 😀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.