A few years ago the idea of a Micro Frontend solution became quite popular. I honestly haven’t stayed up to date with the topic so not sure if it ever went anywhere. I did post this article on dev.to to see what other peoples experience/opinion was on the topic. Looking back at the comments, the 1 user who gave a really detailed comment has either deleted their account or their comment… so thats fun. Anyway, a question I always had was about data sharing. Essentially, if you have a bunch of different isolated apps, possibly in different frameworks, how would you keep shared state synced across the site. For example, user details or authentication state. Maybe 1 app needs to trigger a modal in another (I had this problem recently). The way I solved it there was using the pub/sub design pattern. The modal was enclosed in a widget with a form to filter some data. It was within the navigation and was used to navigate the user to a area in the website, the navigation was managed by AEM and was disconnected from SPA that made up the rest of the page. In this SPA, there was a “Filter” button, once clicked it published an event, the widget subscribed on that event and once it received a message, would open the modal.Documentation Index
Fetch the complete documentation index at: https://adro.codes/llms.txt
Use this file to discover all available pages before exploring further.

useEffects to listen to changes, I just published and subscribed to some events. Luckily this was only needed less than a handful of times so the solution didn’t need to be the most robust, ultra fast, 0.002ms response time type solution.
The way I implementated that was by dispatching a CustomEvent with my data and adding event listeners on components for this event. It meant that I didn’t need to keep a list of subscribers because addEventListener did that for me, and I didn’t need to loop through my subscribers to push them the changes, again, addEventListener does that for me. Have a look at the mandatory “Counter” example on Codesandbox.
This worked out quite well, the project launched and I haven’t thought about it much, until recently. I wanted to experiment with this design pattern a little to communicate between 2 apps in different frameworks. I decided to use React and Vue, because I have experience with both.
I want to make a disclaimer right now. This is a very basic example, I built it without thinking too much. The chances that there is a Micro Frontend state solution is almost a 100%. If you were going to build something custom, I would probably use something like RxJS.
The first thing I did was build a function called
createSubscriptions, this would be used to keep track of subscribers, allow things to subscribe and call a action when the subscribers need to be notified.
subscribe: This method allows things to subscribe to and changes and accepts a callback function which will be the action that is called onpublish.publish: Any part of the application can send out a publish event. We go through each subscriber and call their action.
setCount action, this works out because whenever the publish is triggered it will call setCount with the value. We also return the result of the subscribe method which will unsubscribe the component when it unmounts.
Vue app
publish an updated count.
To see all the code, check out this codesandbox. To play around with the result, check out the preview.
I will say with the codesandbox link, the editor preview is really wacked when using the unpkg version of the libraries. The preview link is a lot nicer.
Something I might eventually play around with is using this pattern but allow it to feel more “native” to the platform (again, I am sure this already exists).

And that concludes my little adventure of using the Pub/Sub pattern to communicate between two different frontend applications. It was quick, it was dirty but I think it works decently well. Definitely something to keep in mind if I ever have another use case for this in a frontend application. Peace! ✌️
Read my latest 'paper'
These 'papers' are bigger articles I write on topics around development and how to do it better.Module Driven Development
Discover the benefits of Module-Driven Development (MDD) in software development. Learn how MDD can improve the efficiency, reliability, and maintainability of large and complex systems. Read the article now to find out how your organization can benefit from MDD.