@actuallab/rpc

中文文档
15.1.24发布于 2026/11/07MITTypeScriptESM + CJS构建来源已验证近 30 天下载 3358

RPC framework for ActualLab TypeScript libraries

原始使用文档
文档目录6 节
  1. Installation
  2. Quick start
  3. API surface
  4. Notes
  5. Documentation
  6. License

@actuallab/rpc

npm Documentation License

TypeScript client (and Node.js host) for ActualLab.Rpc — the WebSocket RPC layer behind Fusion. It speaks the same wire protocol as the .NET implementation, and handles connection management, serialization, streaming, system calls, and automatic reconnection for you.

Use it on its own for plain RPC. For compute calls with server-driven invalidation, use @actuallab/fusion-rpc, which extends this package's RpcHub.

Installation

npm install @actuallab/rpc

ESM-first with a CJS fallback; ships its own .d.ts. Works in browsers and in Node.js (supply a ws WebSocket via peer.webSocketFactory).

Quick start

import { RpcHub, RpcClientPeer, defineRpcService, RpcType } from '@actuallab/rpc';

// The name must match the .NET interface name exactly.
const SimpleServiceDef = defineRpcService('ISimpleService', {
    Greet: { args: [''] },                              // (message) → string
    Counter: { args: [], returns: RpcType.stream },     // () → RpcStream<number>
    Ping: { args: [''], returns: RpcType.noWait },      // fire-and-forget
});

const hub = new RpcHub();
const peer = new RpcClientPeer(hub, 'ws://localhost:5005/rpc/ws'); // auto-starts
hub.addPeer(peer);   // registers it, so hub.close() shuts it down too
const client = hub.addClient<ISimpleService>(peer, SimpleServiceDef);

console.log(await client.Greet('World'));

for await (const i of await client.Counter()) {
    if (i > 10) break;   // sends AckEnd to the server
}

client.Ping('hello');    // returns void, dropped while disconnected

args is only read for its length — the values are placeholders that document the signature. Services can also be declared with the @rpcService / @rpcMethod decorators, which work for both clients and server-side implementations.

API surface

APIDescription
RpcHubPeers, registered services, shared reconnect delayer, addClient / addService / close
RpcClientPeerA client connection with handshake, buffering, and exponential-backoff reconnect (100 ms → 10 s)
RpcServerPeer, RpcWebSocketConnectionHosting side: accept an inbound WebSocket
defineRpcService, rpcService, rpcMethod, RpcTypeService definitions
RpcStream<T>, toRpcStreamAsyncIterable<T> streams with ack-based backpressure and resume-after-reconnect
RpcPeerStateMonitor, RpcPeerStateKindConnection-status state machine with JustConnected / JustDisconnected grace periods, for UI
RpcCallTimeouts, RpcLimitsPer-call timeouts and connection-lifecycle timing (keep-alive, handshake, …)
genericErrorFilterReplaces outbound error messages with a generic one when your host serves RPC

Notes

  • Reconnection is automatic. The delayer lives on the hub and is shared by all client peers; hub.reconnectDelayer.cancelDelays() forces an immediate retry (useful for a "Reconnect now" button). A connection that drops within 15 s of connecting doesn't reset the backoff.
  • Serialization. json5np (default) or MessagePack (msgpack6 / msgpack6c), selected with the ?f= URL key. MemoryPack and polymorphic payloads are not supported — they fail loudly.
  • Node.js. Construct the peer with mustStart = false, set peer.webSocketFactory, then call peer.start().
  • Security. Serve RPC over https/wss: reconnect proofs need crypto.subtle, which is unavailable in insecure browser contexts. If your Node host serves RPC, consider hub.systemCallSender.errorFilter = genericErrorFilter so handler exception text (paths, connection strings) doesn't reach callers.

Documentation

License

MIT — see LICENSE.

版本与发布记录

@actuallab/rpc 共发布 54 个版本,首次发布于 2026/02/18;过去 12 个月发布了 54 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
15.1.24latest2026/11/07今天
15.1.222026/11/07今天
15.1.152026/11/07今天
15.1.112026/11/07今天
15.1.62026/11/07今天
15.0.212026/10/2414 天前

关于 @actuallab/rpc 的常见问题

@actuallab/rpc 是什么?

RPC framework for ActualLab TypeScript libraries

如何安装 @actuallab/rpc?

在项目目录运行 npm install @actuallab/rpc,或使用 pnpm add / yarn add / bun add。

@actuallab/rpc 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@actuallab/rpc 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@actuallab/rpc 可以商用吗?

@actuallab/rpc 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@actuallab/rpc 的下载量有多少?

根据 npm 官方下载统计,@actuallab/rpc 在 2026/10/05 – 2026/11/03 这 30 天内被下载 3,358 次,日均约 112 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@actuallab/rpc 还在维护吗?

最新版本 15.1.24 发布于 2026/11/07(今天);至今共发布 54 个版本,其中过去 12 个月发布了 54 个;首次发布于 2026/02/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。