50-Nodejs Course 2023: Events Driven Architecture: Introduction
Hasan Zohdy
Posted on November 7, 2022
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');
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
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
🎨 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
- Event Driven Architecture: A Practical Guide in Javascript
- Best Practices For Case Styles: Camel, Pascal, Snake, and Kebab Case In Node And Javascript
- After 6 years of practicing MongoDB, Here are my thoughts on MongoDB vs MySQL
Packages & Libraries
- Collections: Your ultimate Javascript Arrays Manager
- Supportive Is: an elegant utility to check types of values in JavaScript
- Localization: An agnostic i18n package to manage localization in your project
React Js Packages
Courses (Articles)
Posted on November 7, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.