promise-postmessage
TypeScript icon, indicating that this package has built-in type declarations

3.5.1 • Public • Published

postmessage-promise

Coverage Status npm version

Simple promise based post message / listener.

Uses MessageChannels under the hood.

Usage

import { onMessage, sendMessage } from 'promise-postmessage';

onMessage(
    async (event) => {
        // some async operation
        const resp = await foo(event.data);
        return resp;
    }, 

    // source frame
    window.parent 
);

const resp = await sendMessage(
    // target frame window.
    document.getElementById('iframe')
        .contentWindow

    // Message payload
    { foo: 'bar' }, 
);

Example: Create a simple forwarding bridge

import { onMessage, sendMessage } from 'promise-postmessage';

const frame1 = document.getElementById("frame1").contentWindow;
const frame2 = window.parent; // Or could be another child iframe.

onMessage((event) => {
    return sendMessage(frame2, event.data);
}, frame1);

onMessage((event) => {
    return sendMessage(frame1, event.data);
}, frame2);

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
3.5.12,688latest

Version History

VersionDownloads (Last 7 Days)Published
3.5.12,688
3.5.09
3.4.02
3.3.02
3.2.02
3.1.12
3.1.02
3.0.12
3.0.02
2.3.02
2.2.02
2.1.02
2.0.32
2.0.22
2.0.12
2.0.02
1.2.02
1.1.02
1.0.02

Package Sidebar

Install

npm i promise-postmessage

Weekly Downloads

2,731

Version

3.5.1

License

ISC

Unpacked Size

96.4 kB

Total Files

27

Last publish

Collaborators

  • ashubham