Emitting events and attaching listeners
Viewers: in countries Watching now:
- Why use Node.js?
- Installing Node.js
- Understanding the event loop
- Initializing Node.js projects
- Creating modules with getters and setters
- Starting Express applications
- Testing your code
- Working with sessions and databases
- Building command-line tools
- Emitting events and attaching listeners
- Controlling readable streams
Emitting events and attaching listeners
Then press Return, and then type npm install. Now let's have a look at the code. Now before continuing, make sure that the connection string in db.js is set to connect to your Mongo DB database. For more information, have a look at the chapter on Mongo DB. I'm going to go to routes and index.js. Currently we have a route for marking flights as arrived. It connects to the Mongo DB database, creates a document, then sends back either a success message or failure status.
Let's consider the following scenario. We may have a web application, where we really don't care about what's happening in the database. This could be more of a non-critical application, where we're simply making a best effort to mark the arrival times. In this case, the speed of our application is more important than making sure the record properly records in Mongo DB. Instead of waiting on Mongo DB to finish inserting the document, we can emit an event and let any number of listeners handle that data. In the meantime, we send a successful status code back to the browser and then handle recording everything else separately.
So let's do that now. First, let's add an event emitter. To do that, I'm going to go to the top of this page. And I'm going to define a new variable called Emitter with a capital E. And to get that, I'm going to require the events module. This is a core module in Node.js, so we don't have to install it. And then I'm going to get the event emitter property from that module. Next, I'm going to create a new emitter. To do that, I'm going to create a new variable called flight emitter, and I'm just going to set it to a new instance of emitter.
Now let's go back down to the function where we're handling the arrivals. So just after marking a flight as being arrived, I'm going to emit an event. I'm going to use that flight emitter that we just created, and I'm going to call the emit method. The first argument is going to be the event type. I'm going to call this an arrival. And then the second argument is the data that we want to pass in this event. I want to pass the flight itself. And now, finally, I want to send the success status right away.
So now, let's add some listeners for this arrival event. The first listener is going to record the data in the database. So I'm going to cut everything from line 44. And it looks like we have an extra response here so I'm just going to remove it. And now, I'm going to go back up to the top of the page and I'm going to create a listener. So to do that, I'm going to call flightEmitter. And then I'm going to call the on method. So now I'm going to create a listener for an arrival event. I'm going to call flight emitter, and I'm going to use the on method.
And then the first argument I'm going to pass is the name of the event, which is arrival. And the second argument I'm going to pass is a function. That function is going to take the flight as it's single argument. So now I'm going to paste in all of that database code. And now I need to make some adjustments. Since we're having the flight passed in right here, I don't need to look it up in an object or an array. So, I'm just going to use that flight object directly. And then finally, I don't need to send any responses. So, I'm going to remove that. And I'm also going to remove this entire else clause.
The only thing that we want to do is log an error on the console if we get one from the database. Let's add another listener. Again I'm going to use flightEmitter. And I'm going to call the on method, and again we're going to listen for arrival events. And in this listener, I'm just going to put the flight number out on the console. So I'll just say flight arrived, and then concatenate the number onto the end of that. Let's go to Terminal and start up the server.
Type in node server and then press Return. Now let's go to the browser and let's fire up Post Man. So in this case, let's mark flight 567 as arrived. So I'm making a put request to localhost at port 3000, slash flight, slash 567, slash arrived. And I am going to send this. It says status success. And then here on the command line, we got that Flight arrived: 567. Now let's go to the arrivals page. Go to localhost at port 3000 slash arrivals.
So we have the arrivals that were marked from before, and the one that we marked just now. And we can also mark another flight as arrived. I'm going to do Flight 577 and then just send that. Successful. It's marked that flight has arrived, 577. If I go back to the arrivals page and hit Refresh, it's now going to list that in the arrivals as well. Event emitters make it possible to send a piece of data to any number of listeners. Since the Functions execute asynchronously, you can emit Events and continue the rest of the code without waiting on the listeners to finish.
In the next video, we'll listen to events on a stream.
Find answers to the most frequently asked questions about Node.js Essential Training .
Here are the FAQs that matched your search "" :
- Q: How can I install the new version of the Express command-line tool?
- A: Newer versions of the Express command-line tool can be installed using the command "npm install -g express-generator." But before doing this, you should first remove any older version of the module, using the command "npm uninstall -g express."
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.