New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

message-event-channel

Package Overview
Dependencies
Maintainers
6
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

message-event-channel

An event driven fault tolerant library for communicating between contexts using MessageChannel.

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7.8K
increased by19.88%
Maintainers
6
Weekly downloads
 
Created
Source

message-event-channel

An event driven fault tolerant library for communicating between contexts using MessageChannel.

Features

  • Subscribe to and broadcast events
  • Send and receive JSON
  • Make requests that return a promise

Installation

Using npm:

npm install message-event-channel --save

Using cdn:

<script src="https://unpkg.com/message-event-channel/dist/message-event-channel.umd.js"></script>

Including

import { ClientConnection } from 'message-event-channel';
const connection = new ClientConnection();

or

const mc = require('message-event-channel');
const connection = new mc.ClientConnection();

or

<script src="https://unpkg.com/message-event-channel/dist/message-event-channel.umd.js"></script>
<script>
  const connection = new mc.ClientConnection();
</script>

Usage

Events

/parent.html

import { ServerConnection } from 'message-event-channel';
const frame = document.querySelector('iframe');
const connection = new ServerConnection(frame);
connection.emit('my-event', {hello: 'world'});
frame.src = "./frame.html";

/frame.html

import { ClientConnection } from 'message-event-channel';
const connection = new ClientConnection();
connection.on('my-event', (payload)=>{
  // {hello: "world"}
  console.log(payload)
});

Request

/parent.html

import { ServerConnection } from 'message-event-channel';
const connection = new ServerConnection(frame);
connection.request('some-data')
  .then(payload => {
    // {hello: "world"}
    console.log(payload)
  })
frame.src = "./frame.html";

/frame.html

import { ClientConnection } from 'message-event-channel';
const connection = new ClientConnection();
connection.on('some-payload', (payload, resolve, reject)=>{
  resolve({hello: 'world'})
});

Emit to all

/parent.html

import { Operator } from 'message-event-channel';
const operator = new Operator();
const connection1 = operator.connect(frame1);
const connection2 = operator.connect(frame2);
const connection3 = operator.connect(frame3);
operator.emit('send-to-all');

Close connection

/parent.html

import { ServerConnection } from 'message-event-channel';
const connection = new ServerConnection(frame);
connection.close();

Options

{
  targetOrigin: '*' // limit the connection to a particular origin (reccomended)
  onload: true, // if the connection should be initialised by an onload event or manually using init()
  timeout: 2000, // default time it takes for requests to timeout
  debug: false, // used to enable useful behind-the-scenes info
  connectionTimeout: 2000 // will trigger the CONNECTION_TIMEOUT event if a connection hasn't been established by this time, can be set to false.
  clientInitiates: false // Server setting - waits for a init() trigger from the child frame before initiating.
}

FAQs

Package last updated on 07 May 2020

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc