Collection

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

The Collection interface of the Room API is a named set of entries that outlives every room, such as the levels players build. Collections are declared under settings.data.collections in irt.config.ts, and room.collection(name) returns one.

An entry has an id, a value, flat meta to filter on, an optional score to sort on, an optional owner, a version, and created and updated times in wall-clock milliseconds. Room code may do anything with any declared collection; the collection’s client settings limit only clients.

Instance properties

Collection.name
The collection’s name. Read only.

Instance methods

Collection.get()
Resolves with an entry and its value, or undefined.
Collection.put()
Creates or replaces an entry.
Collection.list()
Resolves with a page of entries, without their values.
Collection.delete()
Deletes an entry.
Collection.refresh()
Drops what the room holds of the collection’s entries.

Description

  • Values are JSON or a Uint8Array, at most 64 KB. meta is flat strings, numbers and booleans, at most 1 KB. Entry IDs are 1 to 64 of A-Z a-z 0-9 _ . : -. A collection holds at most 1,000,000 entries.
  • The cache. A get() within 5 seconds of the room’s last read or write of the same entry comes from memory. list() is never cached.
  • Errors. Calls reject with a DataError; E_UNKNOWN means the collection isn’t declared in the live build, or is the wrong kind for the call.
  • Limits. A room makes at most 200 data calls a second and 64 at once.

Examples

Publishing a level a player built

// workshop.ts
import { defineRoom } from "@irtio/room";

export default defineRoom({
  async onMessage(room, connection, message) {
    const m = message as { type?: string; tiles?: number[] };
    if (m.type !== "publish" || !Array.isArray(m.tiles)) return;
    const { id } = await room.collection("levels").put({ value: { tiles: m.tiles }, meta: { size: m.tiles.length }, owner: connection.player });
    room.send(connection, { type: "published", id });
  },
});

See also

Runtime availability

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