Skip to content

TypeScript client

AsterClientWrapper calls service methods hosted by another peer. It owns the transport connection, performs consumer admission, and discovers the services the producer offers. Keep it alive while calls and streams are in flight, then close it when those owners finish.

Follow the typed first-service project for package setup, generated service metadata, and matching payload definitions. Connect to the producer’s printed ticket, obtain a client with client(ServiceClass), then invoke its methods with typed request values.

The proxy fragment below demonstrates a separate dynamic workflow. Its interface assertion gives TypeScript application method names; runtime serialization still depends on the producer manifest and codec.

Use the wrapper for consumer admission and discovery over QUIC. A protected producer also requires a provisioned identity or enrollment credential. The placeholder ticket below must be replaced with the producer’s address.

import { AsterClientWrapper } from '@aster-rpc/aster';
const client = new AsterClientWrapper({
address: "aster1...",
});
await client.connect();
try {
// Dynamic proxy: synthesizes XLANG types from the contract
const hello = await client.proxy("Hello") as unknown as {
sayHello(request: { name: string }): Promise<{ message: string }>;
};
const reply = await hello.sayHello({ name: "World" });
console.log(reply.message);
} finally {
await client.close();
}
Option Type Default Description
address string undefined aster1... ticket of the producer to connect to
transport AsterTransport undefined Pre-built transport (for tests). Mutually exclusive with address.
config Partial<AsterConfig> {} Override config values
identity string undefined Path to a .aster-identity TOML file
peer string undefined Peer name to load from the identity file
enrollmentCredentialFile string undefined Path to a .cred enrollment credential for trusted-mode servers
Method Returns Description
connect() Promise<void> Run consumer admission against the producer and discover services
proxy(name) Promise<ProxyClient> Dynamic proxy using manifest-synthesized XLANG types.
client(ServiceClass) Promise<typed client> Typed client built from a generated client class
close() Promise<void> Close the underlying QUIC connections
Property Type Description
connected boolean Whether connect() has succeeded
services ServiceSummary[] Services discovered during admission

For tests and advanced use, createClient builds a typed stub over an existing transport. This is what AsterClientWrapper.client() uses internally.

import { createClient } from '@aster-rpc/aster';
const client = createClient(HelloService, transport);
const result = await client.sayHello(new HelloRequest({ name: "World" }));
function createClient<T extends new (...args: any[]) => any>(
serviceClass: T,
transport: AsterTransport,
options?: ClientOptions,
): AsterClient<InstanceType<T>>;
Option Type Default Description
interceptors Interceptor[] [] Client policy hooks
timeout number undefined Default timeout in seconds for all calls
metadata Record<string, string> {} Default metadata for all calls

Every method accepts an optional CallOptions as the last argument:

const result = await client.sayHello(req, {
metadata: { 'x-request-id': '123' },
deadlineSecs: 30,
});
Option Type Description
metadata Record<string, string> Per-call metadata (merged with defaults)
deadlineSecs number Relative RPC budget in seconds; fractions accepted
serializationMode number Override serialization mode
signal AbortSignal Cancel a live call
version number Service version
sessionId number Connection-scoped session ID
callId number Correlation ID (auto-generated if omitted)

Use a server stream for ongoing updates, a client stream for batches produced incrementally, and bidi for interactive exchange. The following fragments assume matching watchItems, uploadBatch, and Chat.chat contracts and their payload types; the Hello project does not supply those methods.

// Returns AsyncIterable — use for-await-of
for await (const item of client.watchItems(req)) {
console.log(item);
}
// Pass an async generator as the request
const result = await client.uploadBatch(async function*() {
yield new Item({ name: "one" });
yield new Item({ name: "two" });
}());

The typed-client declaration does not accurately describe the bidi channel returned at runtime. Use an already configured transport with the matching service codec/schema for this pattern:

const channel = transport.bidiStream('Chat', 'chat', { deadlineSecs: 30 });
const sending = (async () => {
await channel.send(new Message({ text: 'hello' }));
await channel.close();
})();
const receiving = (async () => {
for await (const reply of channel) console.log(reply);
})();
try {
await Promise.all([sending, receiving]);
} finally {
try { await channel.cancel?.(); }
finally { await Promise.allSettled([sending, receiving]); }
}

close() half-closes request input; responses can continue. Receive concurrently with sending to avoid backpressure deadlocks. Cancel unfinished calls and close the client in a finally block.

AsterClientWrapper has no session() method. Explicit sessions are available through IrohTransport.openSession() and the call option sessionId. Keep IDs on their originating connection. A session transport is a view of the parent connection: closing that view closes the connection and affects sibling sessions. Coordinate its ownership rather than closing each view independently. See the session model.

Pass an AbortController’s signal in per-call options to cancel work. Set deadlineSecs for a native RPC budget; racing a call against a JavaScript timer only limits the local wait. Cancellation cannot undo a committed remote mutation.

RPC status failures use the exported RpcError hierarchy. Inspect status/code rather than matching messages. Retry only idempotent application operations; streaming calls must handle terminal errors after partial delivery.

Transport Use case Package
LocalTransport Testing (in-process, no network) @aster-rpc/aster
IrohTransport Remote peers (QUIC over Iroh P2P) @aster-rpc/aster
// Local (testing)
import { LocalTransport, ServiceRegistry } from '@aster-rpc/aster';
const transport = new LocalTransport(registry);
// Remote peers — requires NAPI native addon
import { IrohTransport } from '@aster-rpc/aster';
const transport = new IrohTransport(connection, codec);