Events in Node.js

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:02:00

What are Events?

The Node.js core APIs use an async event-driven method, utilizing the events module for event handling via the EventEmitter class. Events use a publish/subscribe pattern to process results as they arrive, perhaps delivering partial results before encountering errors, as opposed to callbacks' request/reply pattern, which returns either a result or an error.

Events Module

Node.js includes a built-in module named "Events", which allows you to create, fire, and listen for your own events. To include the built-in Events module, use the require() method. In addition, all event attributes and methods are instances of the EventEmitter object.

Event Emitters

Event Emitters are objects that create events. All objects that emit events are instances of the EventEmitter class. Event Emitters follow the publish/subscribe pattern. The EventEmitter class has Listeners for listing events and Emitters for emitting them.

Event Listener

Event listeners are functions that are called when a specific event occurs. The on() method allows us to register event listeners.

Some Event Listeners

  • addListener: Registers a callback function to be called whenever the specified event occurs.
  • on: Adds an event handler function for the provided event to the target element, as seen in Node.js and several JavaScript frameworks.
  • once: Registers a one-time callback function that is performed when the specified event occurs, and then is automatically removed.
  • removeListener: Deletes a previously registered event handler from an event emitter.
  • removeAllListeners: Removes all listeners for a single event or all events from an event emitter.
  • newListener: An event that is triggered when a new listener is added.
  • error: An error event is emitted when anything goes wrong.

Event Handling

Event emitters are used to manage events. We can design our event emitters by extending the EventEmitter class.

Event Emission

To trigger an event, we use the emit() method of the EventEmitter object. When an event is sent out, all registered listeners for that event are executed.

Event Arguments

When we send an event, we can pass data as arguments. Event listeners can access these arguments.

Error Events

Node.js has a specific event named "error" that handles errors that arise during asynchronous processes. To avoid the application from crashing, errors must be handled properly.

Built-in Events

Node.js core modules and third-party libraries make considerable use of events. For example, the http module generates events like request and response' to handle HTTP requests and responses.

Custom Events

In addition to built-in events, we can create and send custom events within our apps to handle specific circumstances or interactions.

Self-paced Membership
  • 22+ Video Courses
  • 800+ Hands-On Labs
  • 400+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
Accept cookies & close this