Node.js Event Emitters: Practical Examples & Argument Handling Explained

Thapa Technical
63 followers
Dec 3, 2024
Hello what's up guys today in this video we will see event module in our nodejs application and
believe me today's video is very important like one of the most important video
because today we are going to see event module and here like events i hope you understand
in javascript everything is like event only you are clicking on a button so what is onclick
it is an event whenever you submit a form then on submit what is that it is an event
like event is very important in javascript world
so in node event also works right
so here is the same event but in a little different texture
in a little different way
here we don't directly use the event as we could do in javascript
because here we are dealing with the server
but still events are very important and you have to understand
and i will try my best to explain you in the simplest way
so event emitter is a core module in nodejs
used to create and handle custom events
so it's a part of the events module and it's often used for building events driven system in nodejs
event driven system is also a very important term
which is used in many places
if you let me know in the comment section
if you want to know more about this particular event driven system
see this event emitter is basically a class which we will get from the event module
and we will make an instance of it
which will be called anything you can name it but it is widely used in the name of emitter
so when we use the emitter object or basically the instance
when we use that we get many methods in it
in those many methods there are two methods which are widely used
one of them is .emit and the other is .on
you can also call it as add listener
like both are same kind of
so this .emit
suppose you are calling an event or a function
suppose you are calling a function
and you will define a function on
here you will call and here you will define
I have tried to explain you this in the simplest way
but in reality there is a difference
there is a difference between the two
that the way javascript works there
basically the events of javascript are different here
but kind of you understood
emit means you pass the name of the event here and call it
and here you will define it
that whenever someone is emitting this particular event
then I have to listen to that event
and what operation you have to perform you have to write it here
ok so see
purpose
emits or trigger an event with a specified event name
you can also pass arguments that will be consumed by the listeners
it's like calling a function
but instead it triggers all the listeners
attached to the specified event
this is the beauty
so guys let me know in the comment section
let's say I have called a function in javascript
by doing greet
and I have defined that same greet function 5 times
first time, second time, third time, fourth time, fifth time
then when you will call that greet function
will those 5 functions get called
and if there will be only one call
then the one at the top will get called
or the one at the bottom
which you have defined as function greet will get called
let me know in the comment section
but in event emitter
but in node modules
those 5 will get 5 calls
this is the use difference
here those 5 will get 5 calls
ok
and second is your .on method
where attach a listener or a function to a specific event name
this listener will execute when the event is emitted
got it?
remember it in a simple way
here you are calling an event
and here you are defining it
ok just like this
now let me show you
then you will understand
so guys let me do one thing
I come inside this
I create a new folder
by doing events
ok
I create a new file inside this
whose name will be app.js
ok
perfect
ok
const
just a second
first of all write require
this is an event module
so we have events
no problem
equal to
every time we were getting fs from fs module
we were getting os from os module
but when you are playing with events
then you are getting class
that's why we will follow this Pascal case convention
and that is event emitter
ok
this is it
alright
so I have import and event emitter
then we need to create an instance of it
and for some reason
it was not like this
sorry
it is like this
ok
alright guys
you can see
that I have created an instance
then we need to define an event listener
and then we will trigger it
by using this emit
ok
see everything
now
now I have to create an instance
if you have ever dealt with classes
or
oops
or
objects oriented programming
if you have ever done
then you know that
whenever you have to create any instance
then whatever is the name of this class
ok
we have this class name
ok
before that
we need to write the new
and basically
many people call it object
so it's a const
emitter
you have to write
so this is a convention
like this
and yes
here they have added automatic
which is a very good thing
so now guys
these two lines
you have to do it always
first of all
import your class name
event emitter
and then you have to create a new instance
ok
now in this
you have all the methods
of our events module
out of which only two are useful
that is your emit
and then your dot on
or add listener
both are kind of same
now guys
now I have to fire an event
and that is
greet
will be an event
ok
greet is an event
and whenever we will do it
emit
so I need
hello Vinod
or hello Thapa technical
or hello whatever your name is
that's all I need
I
you kind of understand it
that I have to create a function
and whenever I call this function
so this particular whatever name will be
I will get there
ok
so first of all
why not I
define this functionality
I define it
ok
so I will go here
define an event listener
so see how
this is your emitter
inside this when you dot
I mean
when you press dot
automatically gives you a suggestion
BS code
out of which we have this on
either you can write add listener
or you can write on
both are same
ok
after this
now here first of all
you have to pass the name of the event
that which event you want to listen
I am assuming
that my event name will be greet
my event name will be greet
ok
so whenever someone will do this greet function
emit
or trigger
or call
so what do you want to show
now what do you want to show them
what do you want to produce
then
here we need to pass a callback function
right
simple
whenever someone will do this particular event
will do
emit
or trigger
so whatever I will write here
they will see it
so I write simple
console.log
vinod
thapa
simple
right
I wrote simple
now here
I have to console it
I have to greet
now I have to trigger this particular event
or call
ok
how
so again we have this emitter
now you have to play with this emitter
and count that it is only two
either you write on
or you emit it
emit means you produce
right
like
give a signal
this word is something like this
you can say
but I am trying to make you understand like a normal javascript world
where you will understand that you are calling a function
and that function's name is greet
this is it
so emitter
what is this
this is an instance of yours
object
dot
emit
method
in which you are calling an event
that is greet
right
and what are you doing here
you with the help of on event
or on method
you write the name of that particular event here
so that whenever you emit this particular event
or trigger
or call
then this callback function will run
and I will see this
simple
function call
function define
and you understand this simple
but this
although it is not clear
but cd
I have to go out first
cd
dot dot
ok
ls
it's a cd
events
clear
ls
it's an
ok perfect
node
app.js
hello
binod
thapa
how did you like it
this is awesome
that how you are emitting the event here
right
so
now I would like to tell you one more thing here
and that is
here you are hardcoding
but what we can do
is we can also pass
an argument
we can also pass an argument here
see again it's simple
as it was in normal javascript
it's the same here
I will not do anything
I will copy this
I will paste
now what I am doing is
comment
see from here
now what I have to do here
I have to pass an argument
so that this hardcoded binod thapa is not there
I will go here
something like this
simple
now you tell me yourself
that if I pass an argument
and I want it here
then how can I get it
we have seen it thousands of times
if you have done javascript
then it's very easy
when you will read the function
then there is
argument
parameter
we have read all this
right
so let's see
I will write the user's name here
we have already used the template literals
which is a part of ECMAScript
come here
dollar
callibrasis
and pass the user name
our work is done
right
either do this
or do this
but here somewhere
you have the power
that when you are doing the function
emit
then only you are telling
that my name is this
and the same name should be seen here
understand
and understand that
now it is set in one place
now anyone can come and emit it here
as many times as they want
right
so
see
hello binod thapa
how did you like it
this is very awesome
and let's say
you want to add more extra data
now add that too
let's say your profession
I will write
deb
ok
full stack developer
here I will write profession like this
and
hello binod
now I will not say hello here
I will do one thing
wait
here guys I will write
profession
hello
hello binod
right
ok
I mean write anything
I am just telling
that if two come
then how can you use
ok
see
hello binod thapa
you are a full stack developer
right
yes
right
ok
whatever
come
now
here one more thing
I will tell you
now
further
you could have done this in a better way
but it's best idea
to take a single argument
as an object
now what does this mean
see carefully
further
how could you do this better
see
here two user names came
right
now here suppose
I will write more data
so I will have to add
more extra parameters here
I have to add here
I mean
are you understanding
so now what can we do
these two data
now here
you can pass the argument
as a single object
these arguments
you can pass them as a single object
in this way
where
you give
key value pair
my name is binod
my profession is this
so how easy it is
like it's a single object
in that you are passing
your bio data
in the form of key value pair
now here
you don't have to work so hard
you write a single data
let's say
just argument
or say kind of variable
or if you want
say object
no problem
because it's an object only
although it's a parameter
but what you can do
argument
dot
username
argument
dot
your profession
in this way
okay
either you can do this
or you can do this
but it's kind of like
a sim
I mean
it feels a little connected
in this
because it's a single object
in that
it's full value has come
data
now if I go here
then you will get the same
and by the way
hello undefined
you are a full stack
one second
why undefined came here
and double double data is coming
what is the reason behind it
right
nothing is above
okay
that's why undefined
because it's a name
but I am making it username
so it's a username
and it's a profession
now there should be no such problem
so now if I go
and boom guys
see yeah
this this is it
hello Vinod
you are a full stack
right
yeah perfect
see
so guys
this was your emitter
this was your event
module
in your node
yes
and believe me
it's very important
now you understand it
emitter.emit
you have to write the name of the event
and you have to pass the argument
and emitter.on
you have to write the event
first
so yes
this listener
for this particular event
I am writing
you have to give a comma
in the callback function
then you have to define the function
this way it is working for you
it's a little
I mean
funda is different
but it works like this
so guys
finally
now I am in front of you
regarding this particular
event emitter
basically
event module
I am going to bring a challenge
tomorrow
now be ready
and I would like you to perform it yourself
okay
but I hope
you liked the video a lot
and you must have got an idea
that how you can deal with this event module
so guys
how did you like the video
do tell me by commenting
and please like the video
if you have any doubt
I am telling you again
do comment
and we
our next target is
8 lakh subscribers
so if you haven't subscribed yet
please do it
see you in tomorrow's video
till then guys
thank you so much
take care
