

First, we will emit an event with a unique name. Setting an EventEmitter requires four steps. Let’s talk about how we can create such an event handler in the next section. You will have to bind this handler to an event listener which will get invoked as soon the new user event is emitted. You can fire an event whenever a user joins and have an event handler set up that sends a notification the moment this event occurs. We can emit events in any part of the application and have a function setup that listens to it.Įxample: Suppose you have an application where you want to notify the admins of the website whenever a new user signs up.

This bound function is then used to handle the event and perform actions accordingly. What are EventEmitters and why we use them?ĮventEmitter is an object provided by the events module. Let’s learn more about EventEmitters in the section below. This events module gives us EventEmitter which grants us a bunch of operations like emitting events, listening events and capturing event data, etc. It can also be used to perform different actions based on the name and payload of any occurred event. We can use this module to emit events and listen to them. Creating events is called an emitting event and watching for this emitted event is referred to as listening to events. Nodejs comes with a bundled module “Events”, which can be used to create or watch for events. Similarly, if a file is read by an operation, it will be said to be another event. When a user opens a file, we say that an event has occurred. Whenever something happens in the Nodejs world, we call in an event.

We will also learn about memory leaks and learn about ways of dealing with it.
#Nodejs eventemitter how to#
Then we will move on to understand what are EventEmitters and how to use them. Hello everyone, today we are going to talk about events, what exactly are they. Node.js Lesson 9: Events, EventEmitter and Memory Leaks
