From the course: Node.js Essential Training

Unlock the full course today

Join today to access over 24,600 courses taught by industry experts.

Creating a module

Creating a module

- [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 allows us to create listeners for and emit custom events. So let's take a look at it. We're going to do so in this empty file called app js. The first thing we'll do is require events here, and then we're going to create emitter and set that equal to a new events.EventEmitter. Perfect. The next thing we'll do is we're going to use that pattern that we've seen a lot of. We'll say emitter.on, then we will create a custom event and add a callback function here that's going to log whoever the message and the user is. So here we'll say console log, user and message. Excellent, so once we've created this event, we need to emit this event, which we'll do with emitter.emit. We'll use the name of our custom event. We'll use the same string, customEvent, and…

Contents