Project / 2019 / earlier project
RPC
A typed, two-way RPC library that works across different transports and can pass callbacks between peers.
Sending JSON across a worker boundary is easy until the API needs a callback. I built RPC for the point where a browser editor, worker, or process wants to treat the other side like an ordinary typed object.
It supports DOM and Node message ports, with functions allowed in both arguments and results.
The callback crosses the boundary
An editor can ask a worker to find type definitions and receive each file through a callback. The callback lives on the main thread; the worker receives a callable proxy.
const worker = new Worker('./worker.js', { type: 'module' });
const peer = connect<WorkerApi>(Transport.fromDomWorker(worker));
await peer.invoke(
'acquireTypes',
{ react: '^18.0.0' },
(file) => files.set(file.path, file.content),
);
There is no second event protocol for callback messages. The same typed function-shaped API works across the transport.