What are Events?

All actions on a computer, such as clicking a button or opening a file, are considered events. Events allow the developer to create a non-blocking, asynchronous flow of control in their application.

When a script needs to fetch data from a database, it will send the request and continue execution without waiting for a reply. When the data is returned, an event is triggered, and a callback function is executed.

This allows the script to continue executing other tasks while it is waiting for the data to be returned, rather than being blocked. With this, the application is no longer reliant on a traditional blocking, synchronous flow of control.

In Node.js, objects have the ability to trigger events. There are several built-in objects that can trigger events. The most common of such built-in objects include:

  1. EventEmitter: The EventEmitter class allows developers to create custom events and register listeners for those events. This is the base class for many other objects in Node.js.
  2. Stream: The base of the HTTP request and response objects, the Stream class can trigger events when data is received, when a connection is established, and when there is an error
  3. Timers: The setTimeout() and setInterval() functions in Node.js are built on top of the Timers module, which allows the triggering events at specific time intervals.
  4. Child Process: This allows for the creation and management of child processes, a process created by another process to manage subtasks. The Child Process object can trigger events when the child process sends data, sends a message, disconnects, or exits.
  5. File system: The fs module in Node.js can trigger events when a file is opened, closed, or when data is read from a file.

Event Emitter

The Events module in Node.js is a built-in module that allows developers to create, fire and listen for their own custom events. It provides an event emitter class that can be used to create objects that can emit events and register listeners for those events.

To include built-in Events module in our project, we can use the require() method. The module provides an EventEmitter class. To be able to access these properties and methods we’ll need for managing events, we then need to create an instance of the EventEmitter object.

Untitled

This instance of the EventEmitter object can be used to create, emit, and listen for custom events.

The most vital methods to the Event Emitter object are the "emit" method, which is used to fire an event, and the "on" method, which is used to register a listener. The “once” method is notable as well, used to register one-time listeners.

Event Listeners

The ‘on’ method is used to register a listener for a specific event. When an event is triggered by the emit() method, the function called by ‘on’ is executed.

The basic syntax for the ‘on’ method involves calling the on() method through EventEmitter, then entering the name of the event and the function we want to call when an event is triggered: