RoomClient: send() method
@irtio/client 5.0BrowsersNodeThe send() method of the RoomClient interface sends a message to the room, whose onMessage() hook receives it.
Syntax
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 aUint8Array, or aDataView- Sent as a binary message.
- Any other value
- Sent as JSON.
undefinedfields are left out, asJSON.stringify()leaves them.
optionsOptional-
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 tofalse. 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
InvalidStateErrorif the client hasn’t been ready yet, asWebSocket.send()throws while connecting. TypeError- Thrown for a value JSON can’t carry, rather than sending something else: a
Map, aSetor another object with no fields of its own, which would arrive as{}, a function,NaNor an infinity, an object that refers to itself, or bytes inside an object. RangeError- Thrown if
tois 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 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; past 64 they’re dropped. If the client closes instead, they’re dropped. The room handles them after the player rejoins, so its 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() 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 instead.
Examples
Sending a chat message
Each line a player types goes to the room as JSON, which relays it with room.broadcast().
// 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.
// 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
messageevent, for messages from the roomRoomClient.calls, for a message that needs an answerRoom.send(), for the room’s side- Errors and close codes
Runtime availability
| Browsers | Node | |
|---|---|---|
send() | 5.0 | 5.0 |