Demystifying EventEmitter in Node.js
EventEmitter is a class that is built-in to the Node.js runtime. It provides an API for raising and handling events. In this article, we'll take a closer look at how EventEmitter works and how you can use it in your own Node.js applications.
How EventEmitter Works
At its core, EventEmitter is a simple class that provides methods for emitting and listening for events. When you create an instance of EventEmitter, you can use the on() method to register event listeners that will be executed when a specific event is emitted. Here's a simple example:
const EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.on('myEvent', (data) => {
console.log(data);
});
emitter.emit('myEvent', 'Hello, world!');
In this code, we create an instance of EventEmitter and store it in the emitter variable. We then use the on() method to register an event listener for the 'myEvent' event. When this event is emitted, the callback function will be executed and will log the data that was passed to the emitter.emit() method to the console.
Common Internal EventEmitters in Node.js
EventEmitter is used extensively throughout the Node.js core. In fact, many of the built-in classes in Node.js are subclasses of EventEmitter, and they emit events to signal different things to your code.
Here are a few examples of internal EventEmitters in Node.js:
-
The
http.Server
class, which is used to create HTTP servers, emits arequest
event when a client makes a request to the server. The event listener callback for this event takes two arguments: ahttp.IncomingMessage
object representing the request, and ahttp.ServerResponse
object that you can use to send a response to the client. -
The
fs.ReadStream
class, which is used to read data from a file, emits adata
event whenever a chunk of data is available to be read. The event listener callback for this event takes one argument: aBuffer
object containing the data that was read. -
The
net.Socket
class, which represents a network socket (TCP, UDP, etc), emits aconnect
event when a connection is established with the remote peer. The event listener callback for this event takes no arguments.
EventEmitters in Popular Node.js Frameworks
In addition to being used internally in Node.js, EventEmitter is also widely used in popular Node.js frameworks. Here are a few examples of how EventEmitter is used in popular Node.js frameworks:
-
Express.js: A web framework that uses EventEmitter to enable you to create custom middleware that can respond to the
request
event, which is emitted when a client makes a request to the server. The event listener callback for this event takes two arguments: ahttp.IncomingMessage
object representing the request, and ahttp.ServerResponse
object that you can use to send a response to the client. -
Socket.io: A framework for real-time, bidirectional communication between client and server. It uses EventEmitter to enable you to create custom event handlers that can be triggered by the
connection
event, which is emitted when a new client connects to the server. The event listener callback for this event takes one argument: a socket object that represents the connection with the client. -
Koa: A minimalist web framework that uses EventEmitter to enable you to create custom middleware that can respond to the
data
event, which is emitted when data is received in the request. The event listener callback for this event takes one argument: a Buffer object containing the data that was received. -
Hapi: A rich and powerful web framework that uses EventEmitter to enable you to create custom plugins that can respond to the
peek
event, which is emitted when data is "peeked" at in the request. The event listener callback for this event takes one argument: aBuffer
object containing the chunk of data that was "peeked" at. -
Sequelize: An ORM for Node.js that uses EventEmitter to enable you to create custom event listeners that can respond to the
beforeCreate
event, which is emitted just before a new record is created in the database. The event listener callback for this event takes one argument: the object representing the record that is about to be created.