npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

message-channel

v2.0.0

Published

> request and response abstraction for message channel

Downloads

71

Readme

message-channel

request and response abstraction for message channel

The best communication method between an iframe and the main frame is through Channel messaging API.

Build Status

TLDR

Use this library for fast and performant communication between frames which uses message channel api underneath the hood.


why do i need it ?

let's say we want to have few iframes which need to communicate with the main frame.

usualy we'll need to:

  • Listen to post messages from the main frame.
  • Create a message channel from the iframe side.
  • Send a post message with one of the ports to the main frame.
  • Get the message from the iframe and register a callback to messages from the channel.
  • Be able to fit the main frame replies to the requests from the iframe.

with message-channel

// on the main frame
import listenerMessageChannel from 'message-channel/listener';

// the listener will call the onMessage callback everytime a connection is being established.

const handleMessage = (e, reply) => reply(e.data + ' world');

listenerMessageChannel('scope-name', handleMessage);
// on the iframe
import connectMessageChannel from 'message-channel/connect';

// Establish a connection with the main frame and send messages.
const send = await connectMessageChannel('scope-name');

send('hello')
  .then(e => console.log(e.data)); // 'hello world'

Installation

use a scipt tag and import the bundle

<!-- add this on the main frame -->
<script src="node_modules/message-channel/dist/statics/listener.bundle.min.js" />
<script> window.listenerMessageChannel(); // function will be available </script>
<!-- add this on the iframe -->
<script src="node_modules/message-channel/dist/statics/listener.bundle.min.js" />
<script> window.connectMessageChannel(); // function will be available </script>

you can also use npm

npm i --save message-channel

or yarn

yarn add message-channel

basic usage

scope - A scope is way to differentiate between two or more message-channels in the same application. (it has to be the same on both listener and connect)

methods

connectMessageChannel(scope: string, options: object): reply: string

listenerMessageChannel(scope: string, messageHandler: function): void

messageHandler((e: `object`, reply: `function`) => {
  reply(e.data); // will return a reply with the argument value (value must be serializable)
});

important note!: The event you'll get on the onMessage callback is a mirror of the messageEvent type. The reason for not sending the "real" event, is that we add an id to every message and we want to strip the id from the data.

stop the listener

If you want to stop the listener, just call the stop function which returns from the call to the listener.

const stop = listenerMessageChannel('scope-name', (e, reply) => reply(e.data + ' world'));

stop(); // The listener won't work.