# RoomClient: send() method

> The send() method of the RoomClient interface sends a message to the room.

The **`send()`** method of the [`RoomClient`](/docs/client/room-client) interface sends a message to the room, whose [`onMessage()`](/docs/room/room-definition/onmessage) hook receives it.

## Syntax

```ts
send(message)
send(message, options)
```

### Parameters

`message`
: The message to send, at most 64 KB. It may be one of the following types:

  `ArrayBuffer`, a typed array such as a `Uint8Array`, or a `DataView`
  : Sent as a binary message.

  Any other value
  : Sent as JSON. `undefined` fields are left out, as `JSON.stringify()` leaves them.

`options` Optional
: An object with any of these properties:

  `unreliable`
  : A boolean. If `true`, the message is sent unreliably: as a WebTransport datagram, which may be lost or arrive out of order, when WebTransport carries the connection. Defaults to `false`.

  `to`
  : An array of 1 to 16 player IDs. In a native room, the message goes only to those of them who are connected. Any other room closes the connection with 4006 for it.

### Return value

None (`undefined`).

### Exceptions

`IrtError`
: Thrown with the name `InvalidStateError` if the client hasn't been ready yet, as `WebSocket.send()` throws while connecting.

`TypeError`
: Thrown for a value JSON can't carry, rather than sending something else: a `Map`, a `Set` or another object with no fields of its own, which would arrive as `{}`, a function, `NaN` or an infinity, an object that refers to itself, or bytes inside an object.

`RangeError`
: Thrown if `to` is empty or lists more than 16 player IDs.

## Description

After the client was first ready, `send()` doesn't throw for the connection's state. Reliable messages sent while [`readyState`](/docs/client/room-client/readystate) is `"reconnecting"` or `"waking"` wait in the client, up to 64, and go in the order they were sent once the client is open again, before the [`open` event](/docs/client/room-client/open-event); past 64 they're dropped. If the client closes instead, they're dropped. The room handles them after the player rejoins, so its [`onJoin()`](/docs/room/room-definition/onjoin) runs first. Unreliable messages are dropped while reconnecting, as they'd be stale, and every message is dropped after the client closed.

Before sending, `send()` sends this player's unsent owner writes, as [`flush()`](/docs/client/room-client/flush) does, so the room applies them before it handles the message. An unreliable message can overtake the writes sent before it on a real connection.

While the room sleeps, the router holds the connection, and reliable messages reach the room once it wakes. Unreliable messages are dropped meanwhile.

A message over 64 KB, or more than 120 messages a second, closes the connection with 4005, after which the client reconnects with backoff. For a message that needs an answer, declare a call and use [`calls`](/docs/client/room-client/calls) instead.

## Examples

### Sending a chat message

Each line a player types goes to the room as JSON, which relays it with [`room.broadcast()`](/docs/room/room/broadcast).

```ts
// chat.ts
import { connect } from "@irtio/client";

const room = await connect("lounge");
const form = document.querySelector("form")!;
const line = document.querySelector("input")!;

form.addEventListener("submit", (event) => {
  event.preventDefault();
  room.send({ type: "chat", text: line.value });
  line.value = "";
});
```

### Streaming a cursor

Only a cursor's latest position matters, so it goes unreliably: a lost one is replaced by the next. Pointer events can fire more often than the room's limit of 120 messages a second, so the client sends at most one every 50 ms.

```ts
// cursor.ts
import { connect } from "@irtio/client";

const room = await connect("whiteboard");
let lastSent = 0;

addEventListener("pointermove", (event) => {
  if (event.timeStamp - lastSent < 50) return;
  lastSent = event.timeStamp;
  room.send({ type: "cursor", x: event.clientX, y: event.clientY }, { unreliable: true });
});
```

## See also

- [`message` event](/docs/client/room-client/message-event), for messages from the room
- [`RoomClient.calls`](/docs/client/room-client/calls), for a message that needs an answer
- [`Room.send()`](/docs/room/room/send), for the room's side
- [Errors and close codes](/docs/client/errors)
