oRPC Web Workers Adapter

Type-safe oRPC communication between the main thread and Web Workers via the Message Port adapter.

SKILL.md

GitHub

Web Workers Adapter

Web Workers run JavaScript in background threads, preventing UI blocking. Modern runtimes (Bun, Deno) also support them.

With oRPC, you get type-safe communication channels between threads. See the Message Port Adapter for more context.

Web Worker

import { RPCHandler } from '@orpc/server/message-port'
import { onError } from '@orpc/server'

const handler = new RPCHandler(router, {
  interceptors: [onError(error => console.error(error))],
})

handler.upgrade(self, {
  context: {},
})

Main Thread

import { RPCLink } from '@orpc/client/message-port'

export const link = new RPCLink({
  port: new Worker('some-worker.ts'),
})

Vite Web Workers

Vite has first-class Web Workers support:

import SomeWorker from './some-worker.ts?worker'
import { RPCLink } from '@orpc/client/message-port'

export const link = new RPCLink({
  port: new SomeWorker(),
})

For full client examples, see Client-Side Clients.