Events in Node.js

diego.coder26
4 min readOct 2, 2022

Basic article on event handling in Nodejs. Eventemitter object, event emitting, and capture implementations.

Event concept

According to the dictionary, an event is a reference to a scheduled fact or event that can change its circumstances.

Once the concept is understood and taking it to the world of computers, thanks to programming we can handle each of the circumstances that a computer emits, such as processing a file, when we connect a peripheral, or even when a user clicks on the browser (everything can be captured).

Events in Javascript

Events in javascript are the way we have to control actions or behaviors triggered by some entity. In Javascript, we can define programming flows attached when a specific event occurs.

NOTE 🧐: Client and server events have a completely different API, even though they look similar, they have different handlers and features. Mentioning that in this article, we will only address server-side events.

Module Events

Nodejs is composed of an idiomatic asynchronous architecture based on events. This means that the vast majority of Nodejs modules and functionalities work with emission and event capture, such as the File System or HTTP modules, where a large part of their operation is based on event work.

With the support of this module, we can implement the Observer pattern in any part of our application. This is thanks to the Event Emitter class, that just by extending any object of this class, we can practically use all the features of this pattern.

Event Emitter

It is a class within the events module, which provides us with multiple functionalities to work with events, such as the creation of events, management of communication channels, and emission of events, among other features.

Here is an example of an instance that extends Event Emitter incorporating all the functionalities to work with events to the object:

const event = require('events');

class Dog extends event.EventEmitter {}

const dog = new Dog();

Basic event handling methods

The Event emitter class works through callback mechanisms and contains two main methods that help us listen to and publish events:

  • Event Subscription: On.
  • Event Publishing: Emit.

Below is an example of these 2 functionalities:

const event = require('events');
class Dog extends event.EventEmitter { }
const dog = new Dog();

dog.on('bark', () => {
console.log('Woof! Woof!');
});

dog.emit('bark');

The code snippet above prints the following output to the console when the event is emitted:

Woof! Woof!

NOTE 🧐: The communication of the parties must be through the same registered channel or class. Listeners that are separated by different instances will not listen to each other.

Here is an example with 2 different instances listening to the same event on different channels:

const event = require('events');
class Dog extends event.EventEmitter { }
class Cat extends event.EventEmitter { }

const cat = new Cat();
const dog = new Dog();

dog.on('bark', () => {
console.log('Woof! Woof!');
});

cat.on('bark', () => {
console.log('Meow! Meow!');
});

dog.emit('bark');

The code snippet above prints the following output to the console:

Woof! Woof!

Thanks to these two main methods we can practically send and receive information from any point of our application, we only have to subscribe and emit events of our choice. Mentioning that in the events we can also send information as a parameter from one side to the other.

List of most used methods

The events module is extensive, not only are we limited to being able to emit and listen for events, but we can also manage the communication channel, assign and remove listeners.

Here are the most used methods and their brief description:

  • eventNames: Returns an array of strings of the assigned events.
  • once: Event listener that will be triggered only once.
  • removeListener: Remove a specific listener.
  • removeAllListeners: Remove all listeners from the event.

Resources Nodejs events

Ending

Handling events in Nodejs gives us the possibility to understand how other systems or architectures work since they are practically based on the same pub-sub pattern. Mentioning that some NoSQL database engines like Redis already implement this pattern in their core. Opening the possibility of connecting multiple systems based on the same shared communication channel.

Thanks for coming this far, if you find this useful don’t forget to clap 👏. Subscribe to receive more content 🔔.

If you need additional help, please contact me 🤠.

Thank you very much for reading, I appreciate your time.

--

--

diego.coder26

Hello, my name is Diego! 👦, I am Software Engineer that writes about computer science, mathematics, and personal growth. 👉 😃 linktr.ee/diego.coder