defineCalls() function
Available since
@irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room hostThe defineCalls() function declares a room’s calls, the requests a client makes that the room answers, for the calls of defineSchema().
Syntax
defineCalls(declarations)
defineCalls<Signatures>()
defineCalls<Signatures>(parsers)
Parameters
declarations- An object with a
call()declaration for each call, by name. The room checks each call’s args against its fields before the handler runs, and the client checks them before sending. parsersOptional- With a type of signatures, such as
{ upload(args: Uint8Array): number }, each with at most one argument, an object with an optionalparsefunction for each call. Aparsefunction runs on the room only, on the decoded JSON or bytes, and returns the args or throws to reject the call. A call without one gets the decoded JSON unchecked.
Return value
The calls, to pass as calls to defineSchema(). They aren’t part of the schema hash.
Exceptions
TypeError- Thrown for an object that mixes
call()declarations andparsefunctions.
Description
Declarations cover args that fields can describe. Use signatures for args they can’t, such as bytes, a union or an array at the top level. Args that fail their fields or parse reject the call with the args of <name> are invalid: and the reason, and the room doesn’t log it.
Examples
Declaring calls from fields
// trade.ts
import { call, defineCalls, defineRoom, defineSchema } from "@irtio/room";
const schema = defineSchema({
calls: defineCalls({
offer: call({ item: "string", price: "u32" }).returns<{ id: number }>(),
cancel: call({ id: "u32" }),
}),
});
export default defineRoom({
schema,
calls: {
offer: ({ price }) => ({ id: price }),
cancel: () => {},
},
});
Declaring a call by its signature
// emote.ts
import { defineCalls, defineRoom, defineSchema } from "@irtio/room";
const EMOTES = ["wave", "cheer", "shrug"] as const;
type Emote = (typeof EMOTES)[number];
const schema = defineSchema({
calls: defineCalls<{ emote(args: Emote): void }>({
emote(raw) {
if (!EMOTES.includes(raw as Emote)) throw new Error(`must be one of ${EMOTES.join(", ")}`);
return raw as Emote;
},
}),
});
export default defineRoom({
schema,
calls: {
emote(emote, { room, connection }) {
room.broadcast({ type: "emote", player: connection.player, emote });
},
},
});
See also
call()implement()- Calls, the guide
Runtime availability
| Hosted rooms | irt dev | @irtio/testing | Self-hosted room host | |
|---|---|---|---|---|
defineCalls() | 0.1 | 0.1 | 0.1 | 0.1 |