Skip to main content

Function: createRpcClient()

createRpcClient<T>(config): T extends (...args) => I ? RpcAccessible<I> : T & RpcClientProxy

Defined in: packages/rpc/src/client.ts:148

Creates an RPC client that proxies method calls to a remote Durable Object. For the WebSocket transport, connection is established automatically on first method call (lazy connection) and auto-reconnected on first call after disconnect.

Use 'using' for automatic cleanup:

import { createRpcClient, createWebSocketTransport } from '@lumenize/rpc';

using client = createRpcClient<typeof MyDO>({
transport: createWebSocketTransport('MY_DO', 'instance-name')
});
await client.someMethod();
// disconnect() called automatically at end of scope

Or manually manage lifecycle:

const client = createRpcClient<typeof MyDO>({
transport: createWebSocketTransport('MY_DO', 'instance-name')
});
try {
await client.someMethod();
} finally {
client[Symbol.dispose]();
}

Type Parameters

T

T

Either a DO instance type (e.g., RpcAccessible<InstanceType<typeof MyDO>>) or the DO class constructor (e.g., typeof MyDO). When passing a class constructor, instance type with RpcAccessible is inferred automatically.

Parameters

config

RpcClientConfig

Configuration with required transport

Returns

T extends (...args) => I ? RpcAccessible<I> : T & RpcClientProxy

A proxy object with both lifecycle methods and DO method calls

Remarks

This is a factory function that returns an instance of the internal RpcClient class. The factory pattern provides a cleaner API (no new keyword) and allows for easier API evolution without breaking changes. For testing, use createTestingClient which provides sensible defaults for the Cloudflare Workers test environment.

See

Usage Examples