

Pay good attention to the comments in the code.

Using our new-found knowledge of util.inherits(), we will create a Node.js module that extends EventEmitter. The above example was based on an instance of EventEmitter how do we create classes that extend EventEmitter? Node.js has a library named util, which has a method named inherits, which makes it possible to easily extend any class using any other class: var util = require('util') EventEmitter is what makes it possible to write amazing libraries for Node.js. All of those are possible because of the EventEmitter magic. Notice how easy it is to create custom events, add event listeners, emit events, and pass messages via events. Take a look at this example: var EventEmitter = require('events').EventEmitter The best way to show what EventEmitter is capable is through an example. The power of EventEmitter need not be limited to the built-in Node libraries anymore - now it can be yours too! All the event-based Node.js libraries are EventEmitters! In OOP lingo, you might say those Node libraries extend EventEmitter. So a simple example ran on the node REPL: var exampleemitter new ( require ( 'events' ). An event can be 'emitted' (or in other words, the corresponding callback be called) multiple times or you can choose to only listen for the first time it is emitted. The event mechanism work so flawlessly and perfectly, doesn't it. In Node.js an event can be described simply as a string with a corresponding callback. Keep in mind that when an ordinary listener function. You must be familiar with various Node library events like "on data", "on end", "on error", etc. The eventEmitter.emit() method allows an arbitrary set of arguments to be passed to the listener functions. In this tutorial, I will give you a guided demo of the great EventEmitter class. Heard about Node.js' EventEmitter class? Maybe you know that most of the built-in Node libraries use it? Maybe you were always curious about EventEmitter, but had no one to explain it to you?
