0.4.4 • Published 8 months ago

rpc-async-webworker-with-transfer v0.4.4

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

rpc-async-webworker-with-transfer

Two-way RPC between a web worker and the main thread using transferable objects and streams. Streams are available as streamx duplex or readable streams on either end.

See rpc-async API for other implementations and usages.

Package is about 40kb minified.

Installation

npm install rpc-async-webworker-with-transfer --save

Example

import { rpcFromWebWorkerWithTransfer } from "rpc-async-webworker-with-transfer";
import duplexThrough from "duplex-through";
import b4a from "b4a";
const worker = new WebWorker(workerUrl);
const [ourSideDuplex, theirSideDuplex] = duplexThrough();

// main thread
const rpc = rpcFromWebWorkerWithTransfer(worker);
// if in worker
const rpc = rpcFromWebWorkerWithTransfer(self);

// expose functions on either side
rpc.expose({
    connectStream(stream) {
        // stream === theirSideDuplex
        stream.write("some data to the other side");
        
        // you can return a stream it's transferred as well
        return anotherStream;
    },
    largeTextTransfer(largeText) {
        // large text from the other side will be transferred via compact-encoding buffer
    }
});

// request is rpc expecting a response
const anotherStream = await rpc.request.connectStream(theirSideDuplex);

ourSideDuplex.on("data", data => {
    // Get data from the other side.
});

// notify is rpc without expecting response.
rpc.notify.largeTextTransfer(b4a.allocUnsafe(10000));

More Examples


Distributed under the MIT license. See LICENSE for more information.

0.4.4

8 months ago

0.4.2

8 months ago

0.2.55

10 months ago

0.3.21

9 months ago

0.4.1

9 months ago

0.3.2

9 months ago

0.4.0

9 months ago

0.3.1

9 months ago

0.2.53

10 months ago

0.2.52

10 months ago

0.2.51

10 months ago

0.2.5

10 months ago

0.2.4

10 months ago

0.2.1

10 months ago

0.1.0

10 months ago