Blog

  • Home /
  • Blog /
  • Taking Baby Steps with Node.js – Implementing Events

Taking Baby Steps with Node.js – Implementing Events

February 21, 2011

Here are the links to the previous installments:

  1. Introduction
  2. Threads vs. Events
  3. Using Non-Standard Modules
  4. Debugging with node-inspector
  5. CommonJS and Creating Custom Modules
  6. Node Version Management with n

As I already mentioned in one of the previous posts, events lie at the heart of Node.js. In fact, events ARE the heart of Node.js. When building our own custom modules, we are able to make use of this functionality provided by Node.js for emitting our very own events. We can do this by using the EventEmitter exposed by the built-in ‘events’ module. The following code snippet demonstrates how to use the simple API of the EventEmitter.

After creating an eventEmitter object, we subscribe to an event named ‘someOccurence’ and register a function with a message argument to be called when the event is raised. Subscribing is done by executing the on() method of the eventEmitter object. Next we raise our event by simply calling the emit() method of the eventEmitter object, also passing in the value for the message argument of the event handler function.

When providing our own constructor functions exposed by a custom module, we typically want to inherit from EventEmitter. This is also the most common usage throughout the implementations of the built-in modules provided by Node.js. The following example shows how easy it is to turn a constructor function into an event emitter. 

Here we created a constructor function named Downloader. Inside this constructor function we execute EventEmitter.call(this) which simply invokes the EventEmitter constructor function (re)using the current context. We then inherit from the EventEmitter by calling the inherits() helper method of the ‘sys’ module. The implementation of this method looks like this:

This helper method ensures that the prototype methods of the specified superCtor are inherited into ctor. By calling this method we add the prototype methods of the EventEmitter function to our own Downloader function so they can be used by external code.

We also added a download() method to the prototype of our constructor function that emits two different events named ‘status’ and ‘finished’. The first event is raised immediately when the download() method is invoked. The second event is raised after five seconds.

The following code demonstrates how to use our Downloader which we exposed through a module named ‘podcast’.

Here we create a downloader object, register for the two events that we exposed and call the download() method. Instead of adding our constructor function to the exports object, we can also choose to replace the entire reference that is hold by the exports property of the current module as shown in this previous post.

Here we renamed Downloader to Podcast and assigned it to the exports property of our custom module. Now we can use it like so:

This approach looks a bit nicer to me, but that’s just my humble opinion.

As I mentioned earlier, deriving from EventEmitter is a common approach used by many of the built-in modules as well as many of the third-party modules provided by the community. Take a look at the following code that uses the API’s exposed by the ‘http’ module for requesting a page on the web. 

This example simply retrieves the Google main page and prints out the headers to the console when a response is received. The dumbed down implementation of this API looks like this:

var events = require('events');

var eventEmitter = new events.EventEmitter();

eventEmitter.on('someOccurence', function(message){
    console.log(message);
});

eventEmitter.emit('someOccurence', 'Something happened!');

Here we basically see the same approach except that a number of factory methods are  provided in order to easily create and set up the required objects.

Events are essential when building applications with Node.js. Being able to easily use and expose them in your own custom modules is one of the strengths of this great server-side framework.

Until next time.

If you and your team want to learn more about how to write maintainable unit tests and get the most out of TDD practices, make sure to have look at our trainings and workshops or check out the books section. Feel free to reach out at infonull@nullprincipal-itnull.be.

Profile picture of Jan Van Ryswyck

Jan Van Ryswyck

Thank you for visiting my blog. I’m a professional software developer since Y2K. A blogger since Y2K+5. Provider of training and coaching in XP practices. Curator of the Awesome Talks list. Past organizer of the European Virtual ALT.NET meetings. Thinking and learning about all kinds of technologies since forever.

Comments

About

Thank you for visiting my website. I’m a professional software developer since Y2K. A blogger since Y2K+5. Author of Writing Maintainable Unit Tests. Provider of training and coaching in XP practices. Curator of the Awesome Talks list. Thinking and learning about all kinds of technologies since forever.

Contact information

(+32) 496 38 00 82

infonull@nullprincipal-itnull.be