How to use events in Node.js the right way

Usama Ashraf
We’ve moved to freeCodeCamp.org/news
4 min readNov 3, 2018

--

Before event-driven programming became popular, the standard way to communicate between different parts of an application was pretty straightforward: a component that wanted to send out a message to another one explicitly invoked a method on that component. But event-driven code is written to react rather than be called.

The Benefits Of Eventing

This approach causes our components to be much more decoupled. As we continue to write an application, we identify events along the way. We fire them at the right time and attach one or more event listeners to each one. Extending functionality becomes much easier. We can add on more listeners to a particular event. We are not tampering with the existing listeners or the part of the application where the event was fired from. What we’re talking about is the Observer pattern.

https://www.dofactory.com/javascript/observer-design-pattern

Designing An Event-Driven Architecture

Identifying events is pretty important. We don’t want to end up having to remove/replace existing events from the system. This might force us to delete/modify any number of listeners that were attached to the event. The general principle…

--

--

Usama Ashraf
We’ve moved to freeCodeCamp.org/news

Node.js, Rails, Python, C++, React enthusiast. Aspiring software architect.