RoomDefinition: calls hook
@irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room hostThe calls hook of a room definition is an object with a handler for each call the room’s schema declares. A handler runs when a client calls room.calls.name(args), and what it returns is the reply.
Syntax
defineRoom({
schema,
calls: {
name(args, { room, connection }) {},
},
});
Parameters
args- The call’s args. For a call declared with
call(fields), an object with those fields, already checked. For a call declared with a signature, what itsparsefunction returned, or the client’s decoded JSON unchecked when it has none. context-
An object with these properties:
room- The
Room. connection- The
Connectionthat called.
The args come first, as in the function the client called, unlike a hook’s room-first order.
Return value
The reply, or a Promise of it: JSON, or bytes as a Uint8Array, at most 64 KB. A reply of undefined arrives as null. TypeScript checks it against the call’s declared reply type.
Exceptions
If a handler throws, or its promise rejects, the client’s call rejects with the error’s message, as an OperationError. The room keeps running.
- Throw
CallRejectedfor an expected rejection, such as a game rule. It isn’t logged. - Any other error is logged at error level with its stack.
Args that fail the call’s fields or parse reject the call with the args of <name> are invalid: and the reason, before the handler runs, and the room doesn’t log it.
Description
TypeScript reports a declared call with no handler. For handlers kept in their own files, implement(schema, handlers) types them against the schema, to spread into calls with the rest; see Handlers in their own files.
A handler runs as the call arrives, between ticks, like onMessage(), after the caller’s owner writes from before the call are applied. When the handler changed room state, the reply goes out after the state update that carries the change has saved it: at the next tick, or in a room without ticks as soon as the handler returns. So a reply means the change is saved. A reply with no change, and an error, go out at once.
A call in flight when the connection drops or the room restarts fails on the client and isn’t retried. The handler may or may not have run. A call made while the room sleeps is answered after it wakes.
Examples
Voting to skip a map
// vote.ts
import { call, CallRejected, defineCalls, defineRoom, defineSchema, entity } from "@irtio/room";
const Vote = entity("vote", { skips: "u8" });
const schema = defineSchema({
state: Vote,
calls: defineCalls({ skip: call().returns<{ skips: number; needed: number }>() }),
});
export default defineRoom({
schema,
data: () => ({ voted: new Set<string>() }),
calls: {
skip(_args, { room, connection }) {
if (room.data.voted.has(connection.player)) throw new CallRejected("you already voted");
room.data.voted.add(connection.player);
room.state.skips = room.data.voted.size;
return { skips: room.state.skips, needed: Math.ceil(room.connections.size / 2) };
},
},
});
See also
- Calls
defineSchema()onRequest(), for messages from your backend
Runtime availability
| Hosted rooms | irt dev | @irtio/testing | Self-hosted room host | |
|---|---|---|---|---|
calls | 0.1 | 0.1 | 0.1 | 0.1 |