Call a Worker from the Web
Typed RPC calls from a browser with @system-inc/base-client, covering options, retries, and error handling.
@system-inc/base-client is a small transport package for browsers (and workers): construct a client with your service's interface, and every call is checked by TypeScript against the server's own contract.
Construct a client
The generic is the whole trick: NoteServiceInterface is the same interface your worker's service implements (Share Types), so procedure names, argument lists, and return types are all statically checked.
Make calls
client.call() returns the interface itself — call procedures like local methods:
Per-call options ride in call(options):
Retries default to 3 attempts with jittered backoff, retrying transient network failures and 502/503 responses — tune or disable via retry.
Handle errors
A failed procedure throws an RpcError carrying a structured code:
Two details worth knowing:
- Unexpected server errors arrive masked. A handler that throws an unknown error surfaces to the client as
RPC_ERROR_CODE_INTERNAL_ERRORwith'Internal server error'— internals are logged server-side, never leaked to callers. - Need the envelope?
client.callProcedure('getNote', [id], options)skips the proxy and returns the full response (status, callid,durationMs, and the underlyinghttpstatus) useful for instrumentation.
Server prerequisites
For browser calls, the worker needs rpc: { service: { visibility: 'public' } } and a CORS allowlist that admits your web origin — with allowCredentials: true if you send cookies. RPC rides the normal router (POST /__rpc), so the same CORS policy covers it.