NodeJS Event


1. Setting up the Project

Create a new directory for your project and navigate to it in your terminal. Initialize a new Node.js project by running the following command:

This will generate a package.json file for your project.


2. Creating an Event Emitter (EventEmitter)

Create a new JavaScript file, e.g., eventEmitterExample.js, and require the events module at the top of the file:

To create an event emitter, you can extend the EventEmitter class. Here's an example:

In this example, we create a custom MyEmitter class that extends the EventEmitter class. We then instantiate an object of MyEmitter to create our event emitter instance.


3. Listening for Events (on)

To listen for events emitted by an event emitter, you can use the on method. Here's an example:

In this example, we use the on method to listen for the 'myEvent' event. When the event is emitted, the provided callback function will be executed, logging 'Event emitted!' to the console.


4. Emitting Events (emit)

To emit an event from an event emitter, you can use the emit method. Here's an example:

In this example, we use the emit method to emit the 'myEvent' event. When this line of code is executed, the event listener defined in Step 3 will be triggered, logging 'Event emitted!' to the console.


5. Returning an Event Emitter

Sometimes, you may want to encapsulate an event emitter within a function and return it for external use. Here's an example:

In this example, the createEmitter function creates and configures a new event emitter, attaches an event listener to it, and then returns the emitter instance. We can then use the returned emitter to emit events as shown.


6. Inheriting Events (util.inherits or ES6 Class)

To inherit events from one event emitter to another, you can use the util.inherits function (for older versions of Node.js) or extend the EventEmitter class using ES6 class syntax (for newer versions). Here's an example for both approaches:

Using util.inherits (for older versions):

Using ES6 Class (for newer versions):

In both examples, the ChildEmitter inherits the events from the EventEmitter class, allowing it to emit and listen to events just like any other event emitter.

Now, since you know about Event in NodeJS, you can explore more advanced event handling techniques and use cases in your Node.js applications.

FAQs

To create custom events in Node.js, you need to use the built-in EventEmitter class from the events module. First, you create an instance of the EventEmitter, then you can use the .on() method to listen for specific events and the .emit() method to emit (trigger) events. 

You can pass data along with custom events by including additional arguments when emitting the event. These arguments can be accessed in the event listener function's parameter. 

To handle errors with custom events, you can listen for the special error event on the EventEmitter instance. If an error is emitted and not handled by a listener, it will result in a crash.