@pengxs/async-messenger

1.0.1 • Public • Published

async-messenger

🚀 Synchronized communication between windows and iframes

Installing

Package manager

Using npm:

$ npm install @pengxs/asyn-messenger

Using yarn:

$ yarn add @pengxs/async-messenger

Using pnpm:

$ pnpm add @pengxs/async-messenger

Example

parent.html

<html>
<body>
  <iframe id="child" src="child.html" frameborder="0"></iframe>

  <script type="module">

    import asyncMessenger from '@pengxs/async-messenger'

    const child = document.querySelector('#child')
    child.onload = async () => {
      const messenger = asyncMessenger.create({target: child.contentWindow})
      const res = await messenger.post({event: 'say'})
      console.log(res) // Hello World
    }

  </script>

</body>
</html>

child.html

<html>
<body>

  <script type="module">

    import asyncMessenger from '@pengxs/async-messenger'

    const messenger = asyncMessenger.create({target: window.parent})
    window.addEventListener('message', async (e) => {
      const { data, channel } = e.data
      if (data.event == 'say') {
        messenger.send({ channel, data: 'Hello World' })
      }
    })

  </script>

</body>
</html>

License

MIT License © 2023 Mark Peng

Package Sidebar

Install

npm i @pengxs/async-messenger

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

7.5 kB

Total Files

9

Last publish

Collaborators

  • pengxs