50-Nodejs Course 2023: Events Driven Architecture: Introduction
Tags: node, typescript, mongodb, fastify
Published: 2022-11-07 20:45:08

---
title: 50-Nodejs Course 2023: Events Driven Architecture: Introduction
published: true
description: 
series: Nodejs Course 2023
tags: nodejs, typescript, mongodb, fastify
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/idzvlts841og0luw0f7x.png
# Use a ratio of 100:42 for best results.
---

## 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](https://dev.to/hassanzohdy/event-driven-architecture-the-best-paradigm-that-i-love-to-work-with-in-javascript-and-node-js-1gnk).

## 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](https://nodejs.org/api/events.html) 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:

```ts
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:

```ts
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:

```ts
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](https://github.com/hassanzohdy/nodejs-2023)

## 😍 Join our community

Join our community on [Discord](https://discord.gg/pb2vmdfhGf) 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](https://www.youtube.com/playlist?list=PLGO8ntvxgiZMJc7RN2lIq9WmMOlWZGzmz), 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](https://dev.to/hassanzohdy/event-driven-architecture-the-best-paradigm-that-i-love-to-work-with-in-javascript-and-node-js-1gnk)
- [Best Practices For Case Styles: Camel, Pascal, Snake, and Kebab Case In Node And Javascript](https://dev.to/hassanzohdy/best-practices-for-case-styles-camel-pascal-snake-and-kebab-case-in-node-and-javascript-55oi)
- [After 6 years of practicing MongoDB, Here are my thoughts on MongoDB vs MySQL
](https://dev.to/hassanzohdy/after-6-years-of-practicing-mongodb-here-are-my-thoughts-on-mongodb-vs-mysql-574b)

Packages & Libraries

- [Collections: Your ultimate Javascript Arrays Manager](https://dev.to/hassanzohdy/collections-your-ultimate-javascript-array-handler-3o15)
- [Supportive Is: an elegant utility to check types of values in JavaScript](https://dev.to/hassanzohdy/supportive-is-an-elegant-utility-to-check-types-of-values-in-javascript-1b3e)
- [Localization: An agnostic i18n package to manage localization in your project](https://dev.to/hassanzohdy/mongez-localization-the-simplest-way-to-translate-your-website-regardless-your-favorite-framework-4gi3)

 React Js Packages

- [useFetcher: easiest way to fetch data in React Js](https://dev.to/hassanzohdy/usefetcher-the-easiest-way-to-fetch-data-in-react-45o9)

Courses (Articles)

- [React Js: Let"s Create File Manager With React Js and Node Js](https://dev.to/hassanzohdy/lets-create-a-file-manager-from-scratch-with-react-and-typescript-chapter-i-a-good-way-to-expand-your-experience-5g4k)