Overview
Use RPC to communicate easily across contexts in any JavaScript environment.
What is Comctx?
Comctx aims to solve the communication problem between different contexts in a JavaScript environment.
It has a similar goal to Comlink, but it is not trying to reinvent the wheel. Comlink relies on MessagePort, which can be limited in some environments. See Comlink issue #438 for one example.
Comctx is designed to adapt to different JavaScript environments, including Web Workers, browser extensions, iframes, Electron, and other message-based runtimes.
Features
- Environment Agnostic - Works across Web Workers, Browser Extensions, iframes, Electron, and more
- Bidirectional Communication - Method calls and callback support
- Zero Copy - Automatic extraction and zero-copy transfer of transferable objects
- Type Safety - Full TypeScript integration
- Lightweight - Small core size
- Fault Tolerance - Backup implementations and connection heartbeat checks
What Comctx gives you
With Comctx, you can:
- expose a real service in one context
- inject a proxy in another context
- communicate through adapters for different runtimes
- keep a typed API across the boundary
Core model
Comctx is built around three parts:
- Provider - owns the real implementation
- Injector - receives a virtual proxy
- Adapter - connects Comctx to the communication channel
A method call from the Injector is forwarded to the Provider through the Adapter, and the result is returned asynchronously.
Reading path
If you are new to Comctx, read in this order: