Connection: data property

Available since @irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room host

The data property of the Connection interface is an object for the room’s own use, such as the connection’s player entity, kept for as long as the connection is open.

Value

An object, {} when the connection opens. Room code may add any properties to it, or replace it.

Description

data isn’t sent to clients and isn’t saved in room state. It goes when the connection closes, so a player who reconnects starts with {} again, and so does every player after the room hibernates.

When the room goes to sleep, each connection’s data is kept as JSON of at most MAX_SLEPT_DATA (4,096 bytes), and comes back with the connection at the wake. data over that size, or that doesn’t serialize as JSON, wakes as {} and the room logs it. Since it goes through JSON, an entity kept in data wakes as a plain object of its values, not the entity: keep its ID or key there too, or look it up again in onWake. See Sleeping rooms.

Examples

Limiting how often a player can chat

// chat-limit.ts
import { defineRoom } from "@irtio/room";

export default defineRoom({
  onMessage(room, connection, message) {
    if (typeof message !== "string") return;
    const last: number = connection.data.lastChat ?? 0;
    if (room.now() - last < 1000) return;
    connection.data.lastChat = room.now();
    room.broadcast({ from: connection.player, text: message.slice(0, 200) });
  },
});

See also

Runtime availability

Hosted roomsirt dev@irtio/testingSelf-hosted room host
data0.10.10.10.1