Coder Social home page Coder Social logo

comlink's Introduction

Comlink

A tiny RPC library that works on windows, iframes, WebWorkers and ServiceWorkers.

TL;DR: With Comlink you can work on objects from another JavaScript realm (like a Worker or an iframe) as if it was a local object. Just use await whenever the remote value is involed.

$ npm install --save comlinkjs

Comlink allows you to expose an arbitrary JavaScript value (objects, classes, functions, etc) to the endpoint of an communications channel. Anything that works with postMessage can be used as a communication channel. On the other end of that channel you can use Comlink to synthesize an ES6 proxy. Every action performed on that proxy object will be serialized using a simple (and naïve) RPC protocol and be applied to the exposed value on the other side.

Size: ~2.5k, ~1.1k gzip’d.

Example

<-- index.html -->
<!doctype html>
<script src="../../dist/comlink.global.js"></script>
<script>
  const worker = new Worker('worker.js');
  // WebWorkers use `postMessage` and therefore work with Comlink.
  const api = Comlink.proxy(worker);

  async function init() {
    // Note the usage of `await`:
    const app = await new api.App();

    alert(`Counter: ${await app.count}`);
    await app.inc();
    alert(`Counter: ${await app.count}`);
  };

  init();
</script>
// worker.js
importScripts('../dist/comlink.global.js');

class App {
  constructor() {
    this._counter = 0;
  }

  get count() {
    return this._counter;
  }

  inc() {
    this._counter++;
  }
}

Comlink.expose({App}, self);

Module formats

The Comlink module is provided in 3 different formats:

  • “es6”: This package uses the native ES6 module format. Due to some necessary hackery, the module exports a Comlink object. Import it as follows:

    import {Comlink} from '../dist/comlink.es6.js';
    
    // ...
  • “global”: This package adds a Comlink namespace on self. Useful for workers or projects without a module loader.

  • “umd”: This package uses UMD so it is compatible with AMD, CommonJS and requireJS.

These packages can be mixed and matched. A worker using global can work with a window using es6. For the sake of network conservation, I do recommend sticking to one format, though.

API

The Comlink module exports 3 functions:

proxy(endpoint)

proxy creates an ES6 proxy and sends all operations through the channel behind endpoint. The other end of the channel should be passed to expose.

expose(rootObj, endpoint)

expose listens for RPC messages on endpoint and applies the operations to rootObj. The return value will be structurally cloned and sent back. Values that implement the Transferable interface will be transferred.

proxyValue(value)

If structurally cloning a return value is undesired, wrapping the value in a proxyValue call will cause expose to send back a MessagePort instead of the actual value. The MessagePort will be hooked up to a new proxy on the other end.

windowEndpoint(window)

If a window is to be used as an endpoint, the value must be wrapped by windowEndpoint so that messages will be dispatched correctly.

const ifr = document.querySelector('iframe');
Comlink.proxy(Comlink.windowEndpoint(ifr.contentWindow));

License Apache-2.0

comlink's People

Contributors

surma avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.