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

send-channel-message

v1.0.3

Published

Simple function that allow to pass messages to separate script in different browsing context and get back result data as a Promise.

Downloads

21

Readme

send-channel-message

npm version build status test coverage license minified size downloads

known vulnerabilities dependency status dev-dependency status

This package provide simple function that allow to pass messages to separate script in different browsing context and get back result data as a Promise using Channel Messaging API.

Motivation

If you just want to communicate with script in other window/iframe/worker, you can use postMessage function and onMessage event handler.

But if you want to communicate in Promise style, so thats a problem. And Channel Messaging API solves this problem using two-way channels with a port at each end.

Installation

Install the package to your project:

npm install --save send-channel-message

And then you can import the function:

  • If you used ES modules:

    import sendChannelMessage from 'send-channel-message';
  • If you used it in CommonJS environment (don’t forget to add .default to your import):

    var sendChannelMessage = require('send-channel-message').default;
  • Additionally it's also support a UMD build:

    var sendChannelMessage = window.sendChannelMessage.default;

Usage

Basically it's a function that takes 3 inputs and return a Promise:

const promise = sendChannelMessage(message, target, origin);

When Promise resolves it return object with response data:

promise.then( (data) => {
    console.log(data);
});

Inputs

  • message - data to be sent to the other script.

  • target - a reference to the window/worker that will receive the message.

  • origin - specifies what the origin of target must be for the event to be dispatched (default = "*").

*See postMessage syntax description.

Outputs

data property of the MessageEvent interface.

*See MessageEvent data description.

Example

You can create a module for communicating with some iFrame using send-channel-message like this:

/* iframeCommunicator.js */

import sendChannelMessage from 'send-channel-message';

const target = document.getElementById('someIframe').contentWindow;

function sendMessageToIframe(message) {
    return sendChannelMessage(message, target);
}

export default {
    initialize: (initData) => {
        const message = {
            type: 'INIT',
            initData
        };
        return sendMessageToIframe(message);
    },
    // ...
};

And then use it for simple send requests and receive responses in API-like style:

/* index.js */

import iframeCommunicator from './iframeCommunicator';

// ...

iframeCommunicator.initialize('very important data')
    .then( (data) => {
        console.log('Received init response:', data);
    });

// ...

The iFrame script will look something like this:

/* iframe.js */

window.addEventListener('message', ({ data, ports }) => {
    if (data && ports && ports[0]) {
        if (data.type === 'INIT') {
            console.log('Received init request:', data.initData);
            ports[0].postMessage('Init success!');
            ports[0].close();
        }
    }
});

Tests

Simply clone the repo and run

npm install
npm test

License

MIT