Chapter 4. Events: Mastering EventEmitter and beyond

 

This chapter covers

  • Using Node’s EventEmitter module
  • Managing errors
  • How third-party modules use EventEmitter
  • How to use domains with events
  • Alternatives to EventEmitter

Node’s events module currently includes just a single class: EventEmitter. This class is used throughout both Node’s built-in modules and third-party modules. It contributes to the overall architecture of many Node programs. Therefore it’s important to understand EventEmitter and how to use it.

It’s a simple class, and if you’re familiar with DOM or jQuery events, then you shouldn’t have much trouble understanding it. The major consideration when using Node is in error handling, and we’ll look at this in technique 21.

EventEmitter can be used in various ways—it’s generally used as a base class for solving a wide range of problems, from building network servers to architecting application logic. In view of the fact that it’s used as the basis for key classes in popular Node modules like Express, learning how it works can be useful for writing idiomatic code that plays well alongside existing modules.

In this chapter you’ll learn how to use EventEmitter to make custom classes, and how it’s used within Node and open source modules. You’ll also learn how to solve problems found when using EventEmitter, and see some alternatives to it.

4.1. Basic usage

4.2. Error handling

4.3. Advanced patterns

4.4. Third-party modules and extensions

4.5. Summary

sitemap