Node.js Event Emitter Explained in One Minute
Check out all my other posts in My Technical Articles
--
What Is Event Emitter
Event Emitter is a class comes from default events package in Node environment. It is used to emit an event and to invoke a callback function when a certain event happens. It is just like how we manage event in browser JavaScript.
Why Use Event Emitter
Event Emitter is just a necessary class to implement Observer Pattern. Observer Pattern is the key, not event emitter. Wikipedia clearly explains the Observer Pattern that it aims to loose coupling our components so that they could become independent and makes them simpler and easier to manage.
How to Use Event Emitter
When to Use Event Emitter
When there is a need to notify a list of objects when a certain state is changed, we could choose to restructure our code to Observer Pattern to keep loose coupling.
Josedanielhq’s article, Observer Pattern for Dummies with JS, shows how to adopt Observer Pattern using YouTube as example. If you’re not familiar with Observer Pattern, you could give it a try.