RoomClient: calls property

Available since @irtio/client 5.0BrowsersNode

The calls read-only property of the RoomClient interface holds an async function for each call the client’s schema declares, which runs the room’s handler for it and resolves to the handler’s reply.

Value

An object with a function for each call, typed as CallsOf<typeof schema>. Each function has this syntax:

calls.name(args)
calls.name(args, options)
args
The call’s args, as the schema declares them: JSON, or a Uint8Array for a call that takes bytes. A call declared with no args takes undefined or nothing.
options Optional

An object with any of these properties:

timeout Optional
A number: how many milliseconds to wait for the reply. Defaults to 10000.
signal Optional
An AbortSignal that rejects the call when it aborts.

It returns a Promise that resolves with the handler’s reply, JSON or bytes. It rejects with an IrtError:

OperationError
Rejects with this, with code 422, if the handler throws, with the handler’s message as the error’s reason; if the room rejects the args, because they don’t match the call’s fields or its parse() threw; if the args don’t match the fields of a call declared with call(fields), which the client checks before sending anything; or if the room has no such call.
TimeoutError
Rejects with this if timeout passes before the reply, or if a signal from AbortSignal.timeout() aborts.
AbortError
Rejects with this if another signal aborts, with the signal’s reason as cause. The room may still run the call.
NetworkError
Rejects with this if the connection drops or the room restarts before the reply, or if the call is made while the client reconnects.
InvalidStateError
Rejects with this if the call is made after the client closed. Thrown at once, rather than rejected, if the call is made before the client was first ready, as send() throws.

Description

The client sends the player’s unsent owner writes before each call, so the room applies them before it runs the handler. A call made while the room sleeps waits for it to wake and is answered then.

A call that fails with no answer from the room may or may not have run, and the client never retries it. Make calls that change state safe to repeat, or check with another call.

Examples

Buying an item

The shop’s buy call checks the player’s coins in the room, which throws when they’re short. The client tells the player why.

// shop.ts
import { call, connect, defineCalls, defineSchema, IrtError } from "@irtio/client";

const schema = defineSchema({ calls: defineCalls({ buy: call({ item: "string" }) }) });
const room = await connect("main", { schema });

export async function buy(item: string) {
  try {
    await room.calls.buy({ item }, { timeout: 5000 });
    console.log(`bought ${item}`);
  } catch (e) {
    if (!(e instanceof IrtError)) throw e;
    if (e.name === "OperationError") console.warn(`can't buy ${item}: ${e.reason}`);
    else console.error(`the shop didn't answer: ${e.name}`);
  }
}

See also

Runtime availability

BrowsersNode
calls5.05.0