The observer pattern
Another important and fundamental pattern used in Node.js is the observer pattern. Together with the reactor, callbacks, and modules, the observer pattern is one of the pillars of the platform and an absolute prerequisite for using many node-core and userland modules.
Observer is an ideal solution for modeling the reactive nature of Node.js and a perfect complement for callbacks. Let's give a formal definition as follows:
Pattern (observer) defines an object (called subject), which can notify a set of observers (or listeners), when a change in its state happens.
The main difference from the callback pattern is that the subject can actually notify multiple observers, while a traditional continuation-passing style callback will usually propagate its result to only one listener, the callback.
The EventEmitter class
In traditional object-oriented programming, the observer pattern requires interfaces, concrete classes, and a hierarchy; in Node.js, all becomes much simpler...