npm.io
1.0.1 • Published 3 years ago

@randlabs/communication-bridge

Licence
Apache-2.0
Version
1.0.1
Deps
0
Size
20 kB
Vulns
0
Weekly
0

Communication-bridge

Overview

A cross-domain communication library develop by Rand Labs that enables send and receive messages between browser windows/tabs.

Instalation

The library can be installed via npm:

npm i @randlabs/communication-bridge
API Usage

Import and define the communication channel

const Messenger = require("@randlabs/communication-bridge");
const channelName = "channel name";
Tab #1
const client = new Messenger(channelName); // If listener is undefined, it will ignore all incoming request

const window = window.open("https://another.website.com");

const response = await client.sendMessage(window, { customMethod: "doSomething" }, { waitForReply: true, origin: "*" }); // If you are sending sensitive data, put a specific origin, for example: https://another.website.com

console.log(response); // { response: "OK" }
Tab #2
const hub = new Messenger(channelName, function(data, origin, source, sendResponse) {
    console.log(data); // { customMethod: "doSomething" }
    sendResponse({ response: "OK" });
});

See LICENSE file.