Table of contents
My experience in frontend development has equipped me with a keen eye for design aesthetics, user interface (UI) responsiveness, and accessibility standards, all of which I aim to carry forward into the realm of backend development. Lets GooooπͺπΌπ
Fundamentals of nodeJS
Emitting and handling custom events
In order to work with events in nodeJS, we need to import events package from the node. The events is a built-in or we can say co package of nodeJS. Lets import an event and assign to a variable called events using the require function. i.e: const events = require("events")
. This event module gives us an events emitter class. We are going to create an instance of that events emitter class. Lets proceed by creating a variable and assign the instance of that events emitter class. i.e: const myEmitter = new events.EventEmitter()
. The EventEmitter is a class and to create an instance of this EventEmitter class we need to call its constructor. Events emitters emits some named events and when that event is raised or emitted, we can listen to those events. Using this instance we can raise or emit custom events. We use the emit()
to emit our named events. Lets update our code base. ππΌ
The code above will emit an event called userCreated
and when the event is raised, we want to listen for that event using the on()
method which takes some arguments, the events name and a call back function that performs an action when the event is raised.
We are using the emit()
method to emit an event userCreated
and as soon as the event is raised, we listen to that event using the on()
method afterwards the call back function will be executed.
Restart the server and run the code and lets see the output on the console. ππΌ
Using the on()
method we can listen to custom events as well. For that to happen we need to pass some parameters to the call back function. To get the value for these parameters, we pass the value for the parameters as additional argument to the emit()
method. Lets update the code base to new the new output.
Lets see the output on the console. ππΌ
If we have multiple event listeners, they will run synchronously in the order they were declared.
In real world applications, it is best practice to create a new class that will inherit the nodeJS events emitter class.
Lets create a new module file user.js
inside the modules folder. Lets create a class and also export the class. We will import the events
package, the class will also inherit from the events emitter class. Then we create a constructor
and inside the constructor we call the constructor. To call the constructor of the events emitter class we use the super
keyword. All the logic we have in the events emitter class for emitting and listening to events will be available in the user class. lets update the user.js
file. ππΌ
Lets also update our app.js
file by importing the user class. Lets create a variable user and assign the user class imported by specifying the file path. i.e: const user = require("./modules/user")
. So now we can listen and emit events on the user class. Lets update myEmitter variable which was previously: const myEmitter = new events.EventEmitter()
to : const myEmitter = new user()
. So instead of instantiating the emitter class directly we instantiate the user class. So myEmitter
is the instance of the user class. Since the user class internally inherits from the events emitter class we should be able to emit and listen to events. Lets restart the server and run our updated code to see the console output. ππΌ
our code still works perfectlyππΌ
Next: Understanding streams in nodeJS.
See you soonππ«‘βπΌ...