All Projects β†’ franleplant β†’ ibridge

franleplant / ibridge

Licence: other
Typesafe iframe bridge for easy parent child bidirectional communication

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to ibridge

trainmanjs
TrainmanJS - Cross-Origin Communication Library
Stars: ✭ 16 (-36%)
Mutual labels:  communication, iframe, postmessage
post-messenger
πŸ‘Ά ~1 Kb wrapper of window.postMessage for cross-document communication.
Stars: ✭ 28 (+12%)
Mutual labels:  communication, iframe, postmessage
Postmate
πŸ“­ A powerful, simple, promise-based postMessage library.
Stars: ✭ 1,638 (+6452%)
Mutual labels:  iframe, handshake, postmessage
iframe-communication
Basic two way iframe communication
Stars: ✭ 88 (+252%)
Mutual labels:  communication, iframe
chronosjs
JS Channels (Events / Commands / Reqest-Response / Courier) Mechanism
Stars: ✭ 35 (+40%)
Mutual labels:  communication, postmessage
Post Me
πŸ“© Use web Workers and other Windows through a simple Promise API
Stars: ✭ 398 (+1492%)
Mutual labels:  communication, iframe
ember-window-messenger
This aims to be an simple window postMessage services provider.
Stars: ✭ 17 (-32%)
Mutual labels:  communication, postmessage
rx-postmessenger
Minimal RxJS adapter for the window.postMessage API for request-response streams and notification streams across frame windows.
Stars: ✭ 27 (+8%)
Mutual labels:  iframe, postmessage
PostEvent
A Cross-Domain Event Handler javascript library. Pure Vanilla JS, no dependencies.
Stars: ✭ 14 (-44%)
Mutual labels:  iframe, postmessage
DzSocket
Delphi Client/Server components to communicate using TCP Socket Asynchronous easily
Stars: ✭ 44 (+76%)
Mutual labels:  communication
jekyll-loading-lazy
πŸ§™πŸ½β€β™€οΈ Automatically adds loading="lazy" to <img> and <iframe> tags. Load images on your sites lazily without JavaScript.
Stars: ✭ 41 (+64%)
Mutual labels:  iframe
messpostage
PostMessage extension
Stars: ✭ 74 (+196%)
Mutual labels:  postmessage
OpcUaStack
Open Source OPC UA Application Server and OPC UA Client/Server C++ Libraries
Stars: ✭ 89 (+256%)
Mutual labels:  communication
solidproject.org
Website for solidproject.org
Stars: ✭ 115 (+360%)
Mutual labels:  communication
postmessagejs
postmessage-promise is a client-server like, WebSocket like, full Promise syntax (postMessage.then etc.) supported postMessage library. δΈ€δΈͺη±» client-server 樑式、类 WebSocket 樑式、全 Promise θ―­ζ³•ζ”―ζŒηš„ postMessage εΊ“
Stars: ✭ 33 (+32%)
Mutual labels:  postmessage
iFrameX
Iframe generator with dynamic content injection like HTML, Javascript, CSS, etc. and two ways communication, parent <-> iframe.
Stars: ✭ 18 (-28%)
Mutual labels:  iframe
tidymodules
An Object-Oriented approach to Shiny modules
Stars: ✭ 110 (+340%)
Mutual labels:  communication
trident
Trident is a trusted and secure communication platform for enabling better communication between groups of trusted parties
Stars: ✭ 21 (-16%)
Mutual labels:  communication
Channelize-iOS-Chat-SDK-Sample
Open-source JavaScript SDK to enable Real-time Messaging
Stars: ✭ 30 (+20%)
Mutual labels:  communication
cylon-deb
TUI menu driven bash shell script to update and maintain a Debian based Linux distro.
Stars: ✭ 23 (-8%)
Mutual labels:  communication

Node.js CI

ibridge logo

ibridge

A tiny, promise based, type safe library for easy, bidirectional and secure iframe communication.

how ibridge works

Quick start

For this library to work you need to use it both from the parent document and from the child document.

In the child page

// [optional] Define a context that will
// be available to all model functions
const context = {
  vehicles: {
    getCar(carId) {
      return Promise.resolve({ fake: true, carId });
    },
  },
};

// define a model which contains all the functions
// that the parent can remotely call and get their results
const model = {
  vehicles: {
    getCar(carId) {
      const context = this;
      return context.vehicles.getCar(carId);
    },
  },
  // fake model to show how ibridge handles model that throw
  getError() {
    return Promise.reject("fake error");
  },
};

// Instantiate the child
const ichild = new ibridge.Child(model, context);
// await for ibridge handshake with the parent
await ichild.handshake();

In the parent page

// instantiate the parent, this will create an iframe
// element and inject it into the dom (configurable).
const iparent = new ibridge.Parent({
  url: "http://www.child.com"
});

// await for ibridge handshake with the child
await iparent.handshake();

// Done! we are ready to call model functions
const value = await iparent.get("vehicles.getCar", 123);
// => {fake: true, cardId: 123}

try {
  // models can throw in the child and the error will be
  // propagated to the parent
  const value = await iparent.get("getError");
} catch (err) {
  console.log("getError throwed and error in the child")
  console.log(err)
  // => "fake error"
}

πŸ› Debugging

ibridge is written with full debug integration, to enable verbose output in the browser do the following in both child and parent

localStorage.debug = "ibridge:*"

πŸ”Œ Model

model is a great way of modeling a remote function call from parent to child i.e. the parent calls a function that lives inside the child and the child communicates back the return value of that function or throws an error.

model functions can return any serializable value or a promise to a serializable value and they can throw or Promise.reject and the parent will treat them as regular function calls.

Parent can also pass any serializable arguments to the model inside the child.

πŸ—„οΈ Context

context is a really nice way of structuring more complex models in the child, it allows you to share apis, state, socketio connections, configs, across all the model functions.

βœ‰οΈ Free form communication

There might be times where you want more control over the parent child communication, don't worry, you don't need to devolve back to the lower level api of postMessage; ibridge has you covered:

// Send events to the child
iparent.emitToChild("ping", {value: "i am father"})

// listen to events from the child
iparent.on("pong", msg => console.log(msg))
// listen to events from the parent
ichild.on("ping", msg => {
  // send message to the parent
  ichild.emitToParent("pong", {value: "i am child"})
})

Besides the special emitToParent and emitToChild both parent and child are event emitters that extend the Emittery, which means you have much more versatility while building complex event flows between parent and children i.e. once, off, onAny, etc. are all supported, check Emittery docs for more information.

πŸŽ›οΈ Api docs

The library is fully typed, check the code to see all configurations or even let your IDE guide the way.

Max handshake requests

By default ibridge will try 5 times to stablish connection with the child, you can alter this behavior by changing the static attribute:

ibridge.Parent.maxHandshakeRequests = x

Security

Although this library has a handshake mechanism to establish the communication "channel" there is nothing preventing an attacker from using the library or simply simulating the message formats.

The right way of doing this is by working with web standards to define what domains can be parent iframes of our child, this allows you to define an allowlist of secure domains that can use your site inside an iframe.

Please check the mdn documentation.

TLDR: you can define an allowlist of domains that can use your page inside an iframe:

Your page should set the following HTTP header:

Content-Security-Policy: frame-ancestors 'self' www.parent.com *.otherparent.com http://localhost:* ;

If you want to allow any domain then don't set this header.

If your page won't be hosted inside an iframe you want to set this up to 'none' for extra protection.

Logo license

The log is part of twitter emoji.

License: MIT

This is based on postmate, original implementation to the postmate contributors credit.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].