Connection: data property
@irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room hostThe 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
Room.data, for data of the whole room- Where player data lives
Runtime availability
| Hosted rooms | irt dev | @irtio/testing | Self-hosted room host | |
|---|---|---|---|---|
data | 0.1 | 0.1 | 0.1 | 0.1 |