API by package

@threenative/backend API

Every public function and class in @threenative/backend. Cloud saves, shared per-game documents and relay rooms: a local-first client, the Cloudflare Worker handler and the D1 schema behind them.

On this page

Generated from the engine's capability manifest: 3 functions and 5 classes. Each entry is the exported signature, what it is for, and a working example. Start with the package overview.

BackendCollection

class · import { BackendCollection } from "@threenative/backend"

Documents shared inside one game: published levels, shared builds, public profiles.

ts
class BackendCollection

Needs

  • npm i @threenative/backend

Use it to

  • publish a player-built level that other players of the same game can load
  • list the documents other players have made public in this game

Example

ts
const levels = createBackend().db.collection("levels");
await levels.put("canyon", { blocks: 12, title: "Canyon run" }, "public");
const shared = await levels.get("canyon");

BackendDb

class · import { BackendDb } from "@threenative/backend"

The caller's own key/value store: private per player, per game.

ts
class BackendDb

Needs

  • npm i @threenative/backend

Use it to

  • save game progress in the cloud so clearing the browser cache does not lose it
  • keep the player's last write when the network fails, because it lands locally first

Example

ts
const backend = createBackend();
await backend.db.set("save", { level: 4, gold: 20 });
const save = await backend.db.get("save");

BackendError

class · import { BackendError } from "@threenative/backend"

The one failure shape a game handles: the code, plus the server's copy on a conflict.

ts
class BackendError extends Error

Needs

  • npm i @threenative/backend

Use it to

  • branch on a failed save without parsing an error message
  • recover from a stale write by settling on error.value and saving again

Example

ts
const backend = createBackend();
backend.db.set("save", next).catch((error) => { if (error.code === "version_conflict") merge(error.value); });

BackendRooms

class · import { BackendRooms } from "@threenative/backend"

The game's rooms: one call to open one, join one or list what is open.

ts
class BackendRooms

Needs

  • npm i @threenative/backend

Use it to

  • add online multiplayer to a published game without writing a server
  • run a lobby where players join a room by a six-character code
  • find someone to play with by joining the fullest public room
  • send messages between the players of one room, peer to peer when it connects

Example

ts
const rooms = createBackend().rooms;
const room = await rooms.quickJoin();
const open = await rooms.list();

createBackend

function · import { createBackend } from "@threenative/backend"

The cloud store a published game saves into. With no host, no fetch and no localStorage every call still resolves, so a preview build runs the same game code.

ts
function createBackend(options: IBackendOptions =

Needs

  • npm i @threenative/backend

Use it to

  • save player progress in the cloud on a published game so it survives a cache clear
  • share documents between the players of one game
  • keep working in a preview build that has no backend

Example

ts
const backend = createBackend();
await backend.db.set("save", { level: 3 });
const shared = await backend.db.collection("levels").list();

Room

class · import { Room } from "@threenative/backend"

One live room: a socket, its members and the shared state the room has confirmed.

ts
class Room

Needs

  • npm i @threenative/backend

Use it to

  • play a match with another player in the same room
  • carry room messages over a peer-to-peer data channel, with the relay as the fallback
  • check whether a message to a player went peer to peer or through the relay

Example

ts
const room = await createBackend().rooms.create({ max: 4 });
room.send({ move: "e4" }, room.peers[0]);
room.transport(room.peers[0]); // "p2p" or "relay"

deleteGameData

function · import { deleteGameData } from "@threenative/backend/cloudflare"

Removes every row a game owns, in all three tables, and nothing else — what a game or account deletion calls, while revoking a share link keeps the players' saves.

ts
async function deleteGameData(db: IBackendD1, gameId: string): Promise<void>

Needs

  • npm i @threenative/backend

Use it to

  • delete a published game and all of its players' data in one call

Example

ts
await deleteGameData(env.BACKEND_DB, projectId);

handleBackend

function · import { handleBackend } from "@threenative/backend/cloudflare"

Answers one /_backend request: 200 with the body, or { error } with 400/403/404/409/413. Every response carries the marker header, errors included, because the client decides whether a host exists at all by looking for it — an SPA fallback HTML 200 is not a backend.

ts
async function handleBackend( request: Request, options: IBackendHandlerOptions, ): Promise<Response>

Needs

  • npm i @threenative/backend

Use it to

  • mount cloud saves and shared documents on a Cloudflare Worker
  • run the ThreeNative backend on your own Cloudflare account

Example

ts
export default { async fetch(request, env) { return handleBackend(request, { db: env.BACKEND_DB, gameId, playerId }); } }; // gameId and playerId are the host's, never the client's
View source on GitHub ↗