The advanced broadcast message is a simple library that allows you to send messages between tabs of same origin.
This project uses the Broadcast channel and made it promise based to receive response from the other tab.
Note: The sender can receive only one response from the receiver. Hence, if you have multiple receiver, it will receive the response from the fastest responder.
Table of contents
You can install it using npm:
npm install advanced-broadcast-messageOr using yarn:
yarn add advanced-broadcast-messageOr using pnpm:
pnpm add advanced-broadcast-messageInitialize the AdvancedBroadcastMessage class with a channel id. You can use the send method to send messages to the other tab. You can use the on method to listen to messages from the other tab.
You can use the AdvancedBroadcastMessage class to send messages to the iframe.
import AdvancedBroadcastMessage from "advanced-broadcast-message";
const advancedBroadcastMessage = new AdvancedBroadcastMessage("channel-id");
advancedBroadcastMessage.send("message", { data: "Hello, world!" });Use the AdvancedBroadcastMessage class to receive messages in the same project.
import AdvancedBroadcastMessage from "advanced-broadcast-message";
const advancedBroadcastMessage = new AdvancedBroadcastMessage("channel-id");
advancedBroadcastMessage.send("message", { data: "Hello, world!" });
advancedBroadcastMessage.on("message", (event) => {
console.log(event.data); // { data: "Hello, world!" }
});You can return a value from the listener, which will be sent back to the tab.
Note: If you have more than two receiver, the fastest responder will be the one who will send the response back to the sender.
import AdvancedBroadcastMessage from "advanced-broadcast-message";
const iframe = document.getElementById("iframe");
const advancedBroadcastMessage = new AdvancedBroadcastMessage("channel-id");
advancedBroadcastMessage.send("sum", { num1: 10, num2: 11 }).then((sum) => {
console.log(sum); // 21
});
advancedBroadcastMessage.on("sum", (event) => {
return event.data.num1 + event.data.num2;
});You can enable the debug mode to log the messages to the console.
import AdvancedBroadcastMessage from "advanced-broadcast-message";
const iframe = document.getElementById("iframe");
const advancedBroadcastMessage = new AdvancedBroadcastMessage("channel-id", {
debug: true,
});You can use the same AdvancedBroadcastMessage class to communicate with multiple iframes. You can create a new instance of the AdvancedBroadcastMessage class with a different channel id.
Even if the events have the same type, they will not interfere with each other.
import AdvancedBroadcastMessage from "advanced-broadcast-message";
const advancedBroadcastMessage1 = new AdvancedBroadcastMessage("channel-id-1");
const advancedBroadcastMessage2 = new AdvancedBroadcastMessage("channel-id-2");This library is written in typescript, so it comes with its own types. You can use the types to get the type of the payload in the listener and the response from the send method.
import AdvancedBroadcastMessage from "advanced-broadcast-message";
const advancedBroadcastMessage = new AdvancedBroadcastMessage("channel-id");
advancedBroadcastMessage.on<{ from: string }>("message", (event) => {
const data = event.data;
console.log(data.from); // Micheal
return `Hello ${data.from}!`;
});
advancedBroadcastMessage
.send<string>("message", { from: "Micheal" })
.then((response) => {
console.log(response); // Hello Micheal!
});Create a new instance of the AdvancedBroadcastMessage class.
channelId- The channel id to use for the communication.options- The options to use for the communication.options.suppressErrors- A boolean to suppress the error logs. If enabled, the class will not throw an error. This flag is useful when you have a library and you expect some errors. The value is set tofalseby default.options.debug- A boolean to enable or disable the debug mode. If enabled, the class will log the messages to the console.- It also accepts all the options from the Broadcast channel package.
Send a message to another tab.
type- The type of the message.payload- The payload of the message.- Returns a promise that resolves with the response from the that tab.
advancedBroadcastMessage.on(type: string, listener: (event: MessageEvent) => any): { unregister(): void }
Listen to messages from another tab.
type- The type of the message to listen to.listener- The listener to call when a message is received. The listener will receive the event object as an argument. The listener can return a value, which will be sent back to the sender tab.- Returns an object with an
unregistermethod to stop listening to messages.