How to communicate between micro-frontends
Tags: microfrontend, javascript, tutorial, learning
Published: 2023-12-27 00:15:02

## Introduction
In an ideal world, there will be no need to communicate between different micro-frontends, however, in the world where we live-in, there always be some cases where we need to communicate to share data between micro-frontends. But before we dive into the different ways of communicating between micro-frontends, let's take a step back and see on a high level what micro-frontend is.

Micro-frontend is an architectural pattern that advocates breaking down large monolithic frontend applications into smaller, more manageable, easily testable, and deployable applications. It draws inspiration from microservices architecture pattern for backend API.

## Different Communication Ways

### 1. Custom Event
Especially useful in the case of horizontal-split architectural style micro-frontend (check out post for more details on [Micro-frontend Decision Framework](https://dev.to/akdevcraft/micro-frontend-decision-framework-26hf)), as multiple micro-frontends share the same space in a single page view. A custom event is a regular event with a custom body, that provides the ability to define the event name and an optional custom object for the event. The custom event should be dispatched via a common object that is available to all micro-frontends, most likely that common object will be the browser's window object.

```JS
new CustomEvent('myCustomEvent', {detail: {customObj: "SomeData"}})
```

### 2. Cookies
The second option available to share data is cookies, some good use cases to share data via cookies are saving the user's setting to persist volume level or session token. Depending upon data security different flags on cookies can be set like secure, expire (shorter expiry time). Remember, data can only be shared when micro-frontends live under the same sub-domain.

### 3. Web-Storage
Another way to communicate between different micro-frontends is via web storage. Either session or local web storage can be used to share the data, it can be a good use case to store session tokens or less sensitive user data like user's preference. Similar to cookies, data can only be shared when micro-frontends live under the same sub-domain.

### 4. Query Parameter
It can be easily overlooked as an option to share data from micro-frontends but mind it, it's a good option with no complexity. Sometimes we need to follow principles like YAGNI (You Aren't Gonna Need It), KISS (Keep it simple, stupid!) to reduce complexity. It's a good option when we need to share data like item code, product code, etc.

```
https://www.samplesite.com/items/detail?id=007
```

## Conclusion
There are various tools available to communicate or share data between different micro-frontends, however, as a developer we need to decide on when to use which tool. These decisions can be made based upon various factors like the type of micro-frontend architectural styles (horizontal-split vs vertical-split), data security (can it be stored on the frontend or backend) etc. (check out post for more details on [Micro-frontend Decision Framework](https://dev.to/akdevcraft/micro-frontend-decision-framework-26hf))

> If you have reached here, then I made a satisfactory effort to keep you reading. Please be kind enough to leave any comments or ask for any corrections.

## My Other Blogs:

- [How to Reduce Page Loading Time - Part 1](https://dev.to/akdevcraft/how-to-reduce-page-loading-time-part-1-2go1)
- [Avoid Spring RestTemplate Default Implementation to Prevent Performance Impact](https://dev.to/akdevcraft/never-use-spring-resttemplate-default-implementation-2ghj)
- [My firsthand experience with web component - learnings and limitations](https://dev.to/akdevcraft/my-firsthand-experience-with-web-component-learnings-and-limitations-4dd8)
- [Load external font with web component](https://dev.to/akdevcraft/use-font-in-web-component-51a4)
- [Test SOAP Web Service using Postman Tool](https://dev.to/akdevcraft/test-soap-web-service-using-postman-tool-171k)