linka
v2.0.0
Published
A tiny Promise based low-level communication for two way stream
Downloads
8
Readme
linka
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.
:boom: It's works on both Node and Browser. :muscle: 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