This project is under active development.
Until major version released, the API is a subject to change.
A wrapper over Window.postMessage()
and MessageChannel
to facilitate messaging between cross origin contexts.
The library exposes a connection function that returns a Promise
. On a successful connection, the Promise
is resolved with the courier
object to be used to send and listen to messages. Note: the child frame should call the connection function first.
npm i cross-origin-courier
import connect from 'cross-origin-courier';
// ...
connect().then(courier => {
courier.listen(handler);
courier.send({ answer: 42 });
});
function handler(data) {
// do your stuff
}
<script src="https://unpkg.com/cross-origin-courier"></script>
Loaded this way, the connection function is exposed under the following long-ish name
window.createCrossOrigConnection().then(courier => {
// ...
});
const options = {
passphrase: 'foo-bar-baz-qux',
isParent: true,
targetOrigin: '*',
};
createConnection(options).then(courier => {
// ...
});
Option | Type | Default | Description |
---|---|---|---|
passphrase |
string | 'DEFAULT_PASSPHRASE' | todo |
targetOrigin |
string | '*' | todo |
isParent |
boolean | false | todo |
Method | Arguments | Description |
---|---|---|
send |
data <any> |
Sends data to the counterparty |
listen |
dataHandler <(data: any) => void> |
Sets a handler for the counterparty messages |
Please find the full docs here
Copyright © 2021, Sergey Chernykh. Released under the MIT License.
Generated using TypeDoc