Skip to content

A tiny Promise based low-level communication for two way stream

License

Notifications You must be signed in to change notification settings

pathikdevani/linka

Repository files navigation

linka codecov GitHub license build

A tiny Promise based low-level communication for two way stream

Linka is low level communication module. It allows promise based communication on both ends. Using linka you can write wrapper in your application for specific socket, WebSocket, iframe stream.

💥 It's works on both Node and Browser. 💪 Zero dependency

This module exposes three module definitions:

  • CommonJS: dist/linka.js
  • ESModule: dist/linka.mjs
  • UMD: dist/linka.min.js

Install

$ npm install --save linka

Example Linka Implementation for WebWorker

UI side where we create worker instance

import { Linka } from 'linka';

const worker = new Worker('worker.js');
const linka = new Linka(
  (callback) => { worker.addEventListener('message', (e) => { callback(e.data); }); },
  (data) => { worker.postMessage(data); },
  { timeout: 1000 * 10 },
);

// call worker to get some data
linka
  .request('name', { fname: 'foo', lname: 'bar' })
  .then((data) => { console.log(data); }); // foo bar

worker.js

import { Linka } from 'linka';

const self = {};
const linka = new Linka(
  (callback) => { self.addEventListener('message', (e) => { callback(e.data); }); },
  (data) => { self.postMessage(data); },
  { timeout: 1000 * 10 },
);

// bind event
self.bind('name', async (e) => `${e.fname}-${e.lname}`);

License

MIT © Pathik Devani

About

A tiny Promise based low-level communication for two way stream

Resources

License

Stars

Watchers

Forks

Packages

No packages published