JavaScript cross-context RPC

Comctx

Use RPC to communicate easily across contexts in any JavaScript environment.

Web WorkersExtensionsiframesElectronMessage-based runtimes

Worker RPC flow

service.ts
import { defineProxy } from 'comctx'import { streamText } from 'ai'import { openai } from '@ai-sdk/openai'class AiService {  async translate(text: string, targetLanguage: string) {    const result = await streamText({      model: openai('gpt-4o-mini'),      prompt: `Translate to ${targetLanguage}:\n${text}`    })    return result.textStream  }}export const [provideAi, injectAi] = defineProxy(() => new AiService(), {  namespace: '__worker-transfer-example__',  transfer: true})
worker.ts
import Adapter from './adapter'import { provideAi } from './service'const ai = provideAi(new Adapter())
main.ts
import Adapter from './adapter'import { injectAi } from './service'const ai = injectAi(new Adapter())const stream = await ai.translate('Hello world', 'zh-CN')for await (const chunk of stream) {  console.log(chunk)}
Demo

Real-world browser extension RPC.

Features

Core capabilities.

Feature
Environment Agnostic

Works across Web Workers, browser extensions, iframes, Electron, and more.

Feature
Bidirectional Communication

Supports method calls and callbacks across the boundary.

Feature
Zero Copy

Automatically extracts and transfers transferable objects when enabled.

Feature
Type Safety

Keeps the same typed API on both provider and injector sides.

Feature
Lightweight

Ships a small core without forcing a runtime-specific transport.

Feature
Fault Tolerance

Supports backup implementations and connection heartbeat checks.