Room: collection() method
@irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room hostThe collection() method of the Room interface returns a Collection: one of the project’s collections of entries, declared under settings.data.collections in irt.config.ts and shared by every room.
Syntax
collection(name)
Parameters
name- The collection’s name, as
irt.config.tsdeclares it.
Return value
A Collection, whose get, put, list and delete methods return promises. Getting it makes no call, and doesn’t check name: a collection that isn’t declared fails each call with E_UNKNOWN.
Description
A get within 5 seconds of this room’s last read or write of the same entry is answered from memory, so the room sees its own writes at once and other writers’ within 5 seconds. After the room’s own put, the next get of the entry reads it again, since the data service sets the entry’s times. list is never cached. refresh() drops what the room holds of the collection.
Its calls reject with DataError from @irtio/room.
Examples
Saving a player’s track
A racing game keeps the tracks its players build in a tracks collection, so any room can load one.
// workshop.ts
import { defineRoom } from "@irtio/room";
export default defineRoom({
async onMessage(room, connection, message) {
const m = message as { type?: string; pieces?: number[] };
if (m.type !== "publish" || !Array.isArray(m.pieces)) return;
const { id } = await room.collection("tracks").put({
value: { pieces: m.pieces },
meta: { length: m.pieces.length },
owner: connection.player,
});
room.send(connection, { type: "published", track: id });
},
});
See also
Runtime availability
| Hosted rooms | irt dev | @irtio/testing | Self-hosted room host | |
|---|---|---|---|---|
collection() | 0.1 | 0.1 | 0.1 | 0.1 |