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

rimless

v0.4.0

Published

event base communication made easy with a promise-based API wrapping `postMessage`

Downloads

994

Readme

rimless

npm Commitizen friendly npm bundle size

Rimless makes event based communication easy with a promise-based API wrapping postMessage. Works with both iframes and webworkers.

You can use rimless to call remote procedures, exchange data or expose local functions with iframes/webworkers.

You can see it in action in the code sandbox below:

CodeSandbox

Installation

Rimless can be installed via npm.

$ npm i -S rimless

or from a CDN

<script src="https://unpkg.com/rimless/lib/rimless.min.js"></script>

Example Usage

in the host website

import { host } from "rimless";

const connection = await host.connect(iframe, {
  someHostVariable: 12,
  someHostFunction: (value) => `hello ${value}`,
});

// access variables on the iframe
console.log(connection.remote.someGuestVariable); // 42

// call remote procedures on the iframe
const result = await connection.remote.someGuestFunction("here");

console.log(result); // hello here

// close the connection
connection.close();

in the iframe

import { guest } from "rimless";

const connection = await guest.connect({
  someGuestVariable: 42,
  someGuestFunction: (value) => `hello ${value}`,
});

// access variables on the host
console.log(connection.remote.someHostVariable); // 12

// call remote procedures on host
const res = await connection.remote.someHostFunction("there");

console.log(res); // hello there

// close the connection
connection.close();

Getting Started

This is how you can connect your website to an iframe or webworker:

import { host } from "rimless";

const iframe = document.getElementById("myIframe");
const worker = new Worker("myWorker");

// connect to the iframe
host.connect(iframe);

// connect to the worker
host.connect(worker);

You also need to connect your iframe/webworker to the host website.

Usage from an iframe:

import { guest } from "rimless";

// connect to the parent website
guest.connect();

Usage from a webworker:

importScripts("https://unpkg.com/rimless/lib/rimless.min.js");

const { guest } = rimless;

// connect to the parent website
guest.connect();

Exposing an API

To do anything meaningful with this connection you need to provide a schema that defines the API of the host/iframe/webworker. Any serializeable values as well as functions are ok to use. In the example below the host website provides a function that will update its background color when invoked.

import { host } from "rimless";

const api = {
  setColor: (color) => {
    document.body.style.background = color;
  },
};

const iframe = document.getElementById("myIframe");

host.connect(iframe, api);

The api schema must be passed on connection, the same applies to the iframe/webworker.

Calling an RPC

With the host API exposed we can now invoke the remote procedure from the iframe.

import { guest } from "rimless";

// connect returns a promise that resolves in a connection object
// `connection.remote` contains the api you can invoke
guest.connect().then((connection) => {
  connection.remote.setColor("#011627");
});

Closing a connection

Closing a connection will remove all event listeners that were registered.

import { guest } from "rimless";

guest.connect().then((connection) => {
  connection.close();
});

How it Works

  1. The guest (iframe/webworker) sends a handshake request to the host with a schema describing its API
  2. The host confirms the handshake and returns a schema with its own API

Now both can make use of the APIs they have shared with each other, e.g.

  1. The guest requests someAction on the parent.
  2. After verifying the origin, the parent will execute the function mapped to someAction and the result is returned to the guest.

Limitations

All parameters passed through postMessage need to be serializeable. This applies also for all return values of the functions you expose.

// someFunction would return undefined when called in the remote.
const api = {
  someFunction: () => () => {},
};

Alternatives

This library is inspired by Postmate and Penpal.

So why does this library exist?

  • works with webworkers!
  • does not create the iframe (easier to work with libraries like react)
  • works with iframes using srcdoc
  • works with multiple iframes from the same origin

API

Rimless exports two objects: host and guest.

host.connect

Connect your website to a "guest" (iframe/webworker).

host.connect(iframe, {
  log: (value) => console.log(value),
});

| Name | Type | Description | Required | | --------- | ------------------------------- | ------------------------------------ | -------- | | guest | HTMLIFrameElement or Worker | Target of the connection | required | | schema | object | schema of the api you want to expose | - | | options | object | - | - |

guest.connect

Connect a "guest" to your website. The guest connection automatically happens based on the environment it is run.

guest.connect({
  log: (value) => console.log(value),
});

| Name | Type | Description | Default | | --------- | -------- | ------------------------------------ | ------- | | schema | object | schema of the api you want to expose | - | | options | object | - | - |


License

MIT