DEV Community

Cover image for 50-Nodejs Course 2023: Events Driven Architecture: Introduction
Hasan Zohdy
Hasan Zohdy

Posted on

50-Nodejs Course 2023: Events Driven Architecture: Introduction

Introduction

Event Driven Architecture is a software architecture pattern that uses events to trigger actions. It is a very popular pattern in the industry and it is used in many applications. In this course, we will learn how to use it in Nodejs.

To Read more about it, see my Event Driven Architecture: A Practical Guide in Javascript Article.

Usage in our Application

Now let's move to the practical part. We will use it in our application to handle the following use cases:

But first we need an event emitter. We will use the Mongez Events class from Nodejs.

Why? Is it a narcissism? Actually no, but i can assure you that this is the easiest event emitter that you will ever use. It is very simple and straight.

Let's see a simple example:

import events from '@mongez/events';

events.subscribe('delivered', (food) => {
  // this will be called when the event is fired
  console.log('Delivered', food); // Delivered Pizza
});

events.trigger('delivered', 'Pizza'); 
Enter fullscreen mode Exit fullscreen mode

So we basically subscribe to the event (before the trigger) to listen to it, and then we trigger it (after the subscription) to fire the event.

Event Unsubscription

We can unsubscribe from an event by using the unsubscribe method:

import events from '@mongez/events';

const eventSubscription = events.subscribe('delivered', (food) => {
  // this will be called when the event is fired
  console.log('Delivered', food); // Delivered Pizza
});

events.trigger('delivered', 'Pizza');

eventSubscription.unsubscribe(); // once unsubscribed, the callback will not be called anymore

events.trigger('delivered', 'Pizza'); // this will not be called
Enter fullscreen mode Exit fullscreen mode

So far so good, but what if we want to unsubscribe from all events? We can do that by using the unsubscribe method:

import events from '@mongez/events';

events.subscribe('delivered', (food) => {
  // this will be called when the event is fired
  console.log('Delivered', food); // Delivered Pizza
});

events.trigger('delivered', 'Pizza');

events.unsubscribe('delivered'); // once unsubscribed, the callback will not be called anymore

events.trigger('delivered', 'Pizza'); // this will not be called
Enter fullscreen mode Exit fullscreen mode

🎨 Conclusion

In this course, we learned how to use the event driven architecture in our app. We used the Mongez Event Emitter to handle the events.

In our next articles, we'll start implementing events in many places in our application code base.

🚀 Project Repository

You can find the latest updates of this project on Github

😍 Join our community

Join our community on Discord to get help and support (Node Js 2023 Channel).

🎞️ Video Course (Arabic Voice)

If you want to learn this course in video format, you can find it on Youtube, the course is in Arabic language.

💰 Bonus Content 💰

You may have a look at these articles, it will definitely boost your knowledge and productivity.

General Topics

Packages & Libraries

React Js Packages

Courses (Articles)

Top comments (0)