You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

message-channel-promise

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

message-channel-promise

Promise wrapper for communication via post message and MesssageChannel

3.0.2
latest
Source
npmnpm
Version published
Weekly downloads
5
Maintainers
1
Weekly downloads
 
Created
Source

message-channel-promise

Promise wrapper for communication via post message and MesssageChannel

Installation

npm i message-channel-promise

Usage

The module exposes a function which can be used to wrap communication via MessageChannel in a promise, resolved only when the recepient responds.

This works with IFrames and web workers, and message ports.

parameters

  • message: The message to send. Must be a serializable JSON object.
  • target: The contentWindow or worker. can be omitted if sending from worker, since it sends messages to itself.
  • options: contains optional parameters (optional):
    • targetOrigin (optional): The origin to send the message to. Defaults to *, and not necessary for web workers or message ports.
    • transfer (optional): a list of other Transferrable objects to be passed.

You should always send a targetOrigin when working with iFrames.

Examples

Usage with IFrames:

const sendChannelMessage =  require('message-channel-promise');
const frame = document.querySelector('#iframe');
const message = {/* ... */};
sendChannelMessage(message, frame.contentWindow, {targetOrigin: '*'})
  .then(function(data) {
    // Do something with the response
  });

Usage with Web Workers:

const sendChannelMessage =  require('message-channel-promise');
const worker = new Worker(someScript);
sendChannelMessage(message, worker)
    .then(function (data) {
      // Do something with the response
    });

Usage with MessagePort objects:

const sendChannelMessage = require('message-channel-promise');
//get port from somewhere, can also be from other context
sendChannelMessage(message, port)
  .then(function (data) {
    // Do something with the response
  })

Keywords

MessageChannel

FAQs

Package last updated on 27 Jul 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts