Node.js has an event loop that takes care of the flow of execution of the tasks, and when a task gets executed it fires the corresponding event to perform a specific action. Creating a Node.js program using multiple events makes the program run synchronously without interrupting the execution. This type of programming is called event-driven programming.
For example: Suppose we are fetching data from an API, and we are waiting to receive data for executing the rest of the programs, once data is obtained we have associated an event that will run and we have programmed that event to show the fetch data in the console. This way we have done event-driven programming to run the code synchronously until we received the required data.
How Event Works in Node.js
When a Node.js script starts its execution, the variables and methods are first initialised and stored in the memory, then listens come into existence and wait for the happening of an event to perform specific actions. This event can be built-in or user-defined. This event-driven nature of Node.js makes it super fast compared to other technologies.
Node.js Event-Driven Programming
Node.js has a module named “event” with a class EventEmitter used for Event-Driven Programming in Node.js.
Importing Event Module
To use this module, it is not required to install it, as it comes built-in with Node.js, you just have to import it.
Below is the syntax to import the event module in Node.js projects:
const events = require('events');
Note: This tutorial does not cover the event module and its method entirely, Event module has multiple methods which can be used for performing many types of operations, we have a separate tutorial on it, for reading it, click here.
EventEmitter Object
EventEmitter Object is created using EventEmitter class from the “event” module we imported earlier. This EventEmitter Object is used for further Event-Driven Programming.
Below is the syntax to create an EventEmitter Object:
const eventEmitter = new events.EventEmitter();
Here the eventEmitter object created has many methods, such as “on” method to create and bind an event to the handler and an emit method to fire an event.
Creating an Event
Below is the syntax to create an event:
eventEmitter.on(eventName, eventHandler);
Where,
- eventName is used to run the eventHandler function associated with it,
- eventHandler is a callback function that decides the action to perform once its corresponding event is triggered.
Fire an Event
Below is the syntax to fire an event:
eventEmitter.emit(eventName);
Where,
- eventName is the name of the event we have to fire.
Note: Additionally we can also pass arguments after eventName followed by a comma if the corresponding event takes arguments.
Example of Event-Driven Programming in Node.js
Let’s see an example of the above implementation to understand Node.js event-driven programming better.
In this example, we will stimulate an application that connects two people randomly, it waits for the connection and once connected it triggers an event “match” which shows the message “Match Found” in the console.
Note: We will not focus on the functionality to connect people, we are just assuming it to understand how event-driven programming works.
app.js
const events = require('events'); // import events module
const eventEmitter = new events.EventEmitter(); // create an eventEmitter object
eventEmitter.on('match', () => { // bind the connection event with the handler
console.log('Match Found');
});
eventEmitter.emit('match'); // fire the event
Run The Code:
To run the above code, execute the below command in the terminal.
node app.js
Output:
Match Found
Summary
Event-driven programming is a way to write code using events. The events make execution synchronous in a required sequence while increasing the overall execution speed. In this tutorial, we have covered Node.js event-driven programming. Hope you like it.
Reference
https://nodejs.org/api/events.html