- [Instructor] A powerful tool that ships with Node.js
is the Event Emitter.
The Event Emitter is Node.js' implementation
of the Pub-Sub design pattern,
and it gives us a mechanism for emitting custom events,
and wiring up listeners and handlers for those events.
Let's go ahead and mess around with the Event Emitter
by creating a new file within our samples called events.js,
and within this new file the first thing I'm going to do
is get our Events module, so we will create a new variable
called events and we will require our Events module.
So I don't need to put a path to this module,
because it is a core module that comes with Node.js.
The next thing I need to do is create
an instance of the Event Emitter,
so I'm going to create something called the emitter,
and the emitter is going to be a new Events Event Emitter.
So the Events module comes with an Event Emitter constructor
that will construct a new instance of the Event Emitter
that we can use to raise custom events.
Well, how do we raise a custom event?
We use the emitter's emit function.
And the first argument that this function takes
is the string name of the event.
We can call our events whatever we like,
but they should make sense to the user.
I'll call this customEvent.
So here we're going to raise a custom event.
The next set of arguments represents the data
that I want to pass to the handler of the custom event,
so we'll go ahead and pass a "Hello World",
and the term computer.
The idea behind this is we're going to say
that the computer has said "Hello World".
So the power of the Event Emitter is we can raise,
in our code, events, wherever we like.
So I'll raise another custom event,
and we'll say "That's pretty cool, huh?"
and we'll say that Alex is the one that said this, great.
So these events are being raised within our code,
what we need to do next is handle them.
So using the emitter's .on function,
I can wire up a handler or a function
that will handle custom events.
So we can say when a custom event occurs,
handle it with this callback function.
There's a little arrow function here,
and within this arrow function,
we will take in the argument.
So the first argument that is sent to this callback function
is the second argument of the emit.
So this is our message.
And the next argument would be the third argument
of the emit function, and so on and so on.
So in this case we only have two arguments,
so the message and the user are being passed
to the callback handler,
and from within the callback handler,
we can go ahead and log
user colon message.
Great, so I can save this file
and I'll go ahead and open up the terminal.
And we can go ahead and run this by typing node events.
And we can see two events have been raised,
one on line nine, and one on line 10.
Both events are handled by the same handler function
that we registered on line five.
So the Event Emitter gives us a way
to raise and handle custom events.
One other important feature of the Event Emitter
is that it's asynchronous.
These events are raised when they happen.
Let's just do a quick example of this by listening
for user input from the terminal.
So we'll say process.stdin.on data,
and when we receive that data it'll be passed
to this callback function,
so whenever the user types anything we will collect it here,
and I'll create a variable called input
where we take the data, convert it to a string,
trim that string, so that's what the user has typed,
and we can emitter.emit our custom event.
And from this custom event,
we can say that the message is the input,
and that the terminal user is the one who added the message.
And we'll go ahead and add one more thing to this function,
so we'll do an if statement,
if the input happens to be equal to exit,
we can do a process.exit,
but right before we exit we can also raise another event,
so I can say emitter.emit another custom event,
and this time we can say "Goodbye!"
from the process.
Great!
So let's go out to our terminal and run our new example.
Say node events.
And as you can see, we just have a listener,
so we're listening for any data
that's input into the terminal, so if I say "Hello World",
we see the terminal has said "Hello World".
If I say "Node js is neato",
we see that the terminal says "Node js is neato",
and then when I type "exit",
we see that we exit the process,
but the process says "Goodbye!" right before then.
So the Event Emitter gives us a way
to raise custom events that we can wire listeners
or handlers to using the on function.
In this example, every time we hit line 15 or line 12
within this code, a custom event is raised
and some data is passed to that event.
On line five, we actually handle all of our custom events
by using the on function and retrieving the data
that was passed to that event, and the callback function.
And in our case we're just displaying that to the user
in the terminal.