# RoomDefinition: calls hook

> The calls hook of a room definition is an object with a handler for each call the room's schema declares.

The **`calls`** hook of a [room definition](/docs/room/room-definition) is an object with a handler for each call the room's [schema](/docs/room/room-definition/schema) declares. A handler runs when a client calls `room.calls.name(args)`, and what it returns is the reply.

## Syntax

```ts
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 its `parse` function returned, or the client's decoded JSON unchecked when it has none.

`context`
: An object with these properties:

  `room`
  : The [`Room`](/docs/room/room).

  `connection`
  : The [`Connection`](/docs/room/connection) that 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 `CallRejected` for 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](/docs/room/guides/calls#handlers-in-their-own-files).

A handler runs as the call arrives, between ticks, like [`onMessage()`](/docs/room/room-definition/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

```ts
// 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](/docs/room/guides/calls)
- [`defineSchema()`](/docs/room/defineschema)
- [`onRequest()`](/docs/room/room-definition/onrequest), for messages from your backend
