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.11,678latest

Version History

VersionDownloads (Last 7 Days)Published
3.5.11,678
3.5.028
3.4.01
3.3.00
3.2.00
3.1.10
3.1.00
3.0.10
3.0.00
2.3.00
2.2.00
2.1.00
2.0.30
2.0.20
2.0.10
2.0.00
1.2.00
1.1.00
1.0.00

Package Sidebar

Install

npm i promise-postmessage

Weekly Downloads

1,638

Version

3.5.1

License

ISC

Unpacked Size

96.4 kB

Total Files

27

Last publish

Collaborators

  • ashubham