Watch and Handle Changes with Web Storage Api — Cross Tab Communication

Mustafa Dalga
heybooster
Published in
2 min readJul 3, 2022
Photo by Markus Spiske on Unsplash

Heybooster is a client-based project. The data in the Heybooster changes depending on the client.

The user can see different client information on different browser tabs under normal circumstances. However, we don’t want to show different client information in different browser tabs. Therefore, our goal is to synchronize the active client across all browser tabs.

To solve the problem , we listened to the active client with the web storage api and handled the client change status.

I created a demo project to explain how to solve our problem. You can check it at the end of this blog post.

In the code below, we have defined the first client as activeClient by default when there is no activeClientID. We created an action to change activeClientID. This action will change the status of the activeClientID store and localstorage.

By using the function below, when the activeClientID is changed, we automatically refresh the page in all other tabs so that they receive the same activeClientID.

To use the above codes, we import them into the App.vue file.

  • We obtained the client and activeClientID information from the store and displayed them in the dropdown.
  • We called the changeActiveClientID action when the client is changed.
  • With the changeActiveClientID action, activeClientID information will be changed in the store and localStorage, and the page will be refreshed in other tabs.
  • We called activeClientIDListener to listen to client change status in other tabs.

Demo project: https://cross-tab-communication.netlify.app/

Demo project code: https://github.com/mustafadalga/cross-tab-communication

Originally published at heybooster.

--

--

Mustafa Dalga
heybooster

Software Developer | Nature and Animal Photographer