cft

Sending messages to another tab using Js

Sending messages to another tab using Javascript. Here's how you can do it...


user

Anjal Binayak

2 years ago | 1 min read

Communicating between 2 tabs opens up exciting opportunities while creating web applications. Before we dive into its use cases, let's learn to send and receive messages between two tabs.

We should instantiate BroadcastChannel with a channel_name as a parameter:

After instantiating, we can listen for event message

we can use postMessage method, to broadcast the message to all the tabs connected to the same channel

Some of the use cases of this are:

  • Detect user actions in other tabs
  • Know when a user logs into an account in another window/tab.
  • Instruct a worker to perform some background work
  • Know when a service is done performing some action.

Example: If use logouts in one tab, we can automatically logout user in all the tabs

This article was originally published on https://dev.anjalbinayak.com.np/js-broadcast-channel-api

Upvote


user
Created by

Anjal Binayak

šŸ‘Øā€šŸ’» ReactJs Developer šŸ›  Building Tools & Saas ā› React, Next, Node, WordPress


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles