DEV Community

Cover image for Event Driven Architecture, the best paradigm that i love to work with in javascript and Node js
Hasan Zohdy
Hasan Zohdy

Posted on

Event Driven Architecture, the best paradigm that i love to work with in javascript and Node js

Introduction

Event-driven architecture (EDA) is a software architecture paradigm promoting the production, detection, consumption of, and reaction to events.

What is Event driven architecture in simple words

Well, EDA is a way to communicate between decoupled services that doesn't know each other directly, but some of them waiting the other to occur to do something.

When the Door bell rings

Let's take a simple example, your mother says to you when the door bell rings, open the door and surely you'd say Yes mom, so you're waiting the door bill to ring so you can go up from your lazy couch and open up the door.

Now the door bill is ringing, so you now opened the door and case closed!

That's is how the EDA works.

DOM Events

Being reading this article, it surely means that you're already familiar with javascript, the best example to get to know more about events is the Events listeners in javascript in DOM.

So we want to display "Welcome User" when the user clicks on a button.

<button onclick="alert('Welcome User')">
Click me
</button>
Enter fullscreen mode Exit fullscreen mode

As we can see, we display an alert message only and only when the user clicks on the button.

EDA Workflow

So we got to know the concepts and the basics of EDA, let's see how it works in basic steps.

There are three parts for any event system:

  1. The Event name itself (The door bell rings).
  2. The Event listener (you when you were waiting the door bell to ring).
  3. The Event trigger (When the door bell rings).

Thus we say that when the door bell rings (The Event) occurs, you go up and open the door (The Event Listener), which will happen in the next 15 minutes for example (The Event Trigger or Event Action).

Based on the previous steps, we can conclude that you MUST listen to an event before it occurs, which is obviously make sense as your mom won't tell you to open the door after the door bell rang 30 minutes ago, simply put, the order matters!

EDA Terminologies that you probably should know

There are some terminologies that you may see when you read an article or watch video about EDA, i will share with you some of it so you don't get confused.

  • For Event Listeners, It is equivalent to Event Subscribers Event Consumers, event consumers subscribe to the event.
  • For Event Trigger, its also called Event Producer or Event Publisher

So you might read the Pub/Sub terminology in some framework pub is a shorthand for Publish or Publisher and sub is a shorthand for Subscribe or Subscriber.

Real World Examples of using EDA

Apart from Handling DOM Events, there are multiple Real World Examples that EDA is main hero inside it.

For instance, chat messaging, a user sends a message, and the other user waits for it, once the message is sent to the server, the server sends it to the other user, the other user is subscribing to the user's channel to receive a message, this is simply the main concept beyond chat messaging.

Also any data management/bus that is being transferred in Sockets implements EDA by default.

I myself use it in many of my packages, Forms, Ajax Requests, Atoms, i even have a standalone Event package that i use aggressively in almost all of my projects either in Backend or Frontend projects.

Conclusion

The Event Driven Architecture is a powerful paradigm to mange you data between different layers, models or even different and separate apps.

I hope you enjoyed the article and it was clear enough to you :)

Have a nice day and happy Coding :)

Top comments (0)