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
Documents shared inside one game: published levels, shared builds, public profiles.
class BackendCollectionNeeds
- 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
const levels = createBackend().db.collection("levels");
await levels.put("canyon", { blocks: 12, title: "Canyon run" }, "public");
const shared = await levels.get("canyon");BackendDb
The caller's own key/value store: private per player, per game.
class BackendDbNeeds
- 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
const backend = createBackend();
await backend.db.set("save", { level: 4, gold: 20 });
const save = await backend.db.get("save");BackendError
The one failure shape a game handles: the code, plus the server's copy on a conflict.
class BackendError extends ErrorNeeds
- 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.valueand saving again
Example
const backend = createBackend();
backend.db.set("save", next).catch((error) => { if (error.code === "version_conflict") merge(error.value); });BackendRooms
The game's rooms: one call to open one, join one or list what is open.
class BackendRoomsNeeds
- 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
const rooms = createBackend().rooms;
const room = await rooms.quickJoin();
const open = await rooms.list();createBackend
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.
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
const backend = createBackend();
await backend.db.set("save", { level: 3 });
const shared = await backend.db.collection("levels").list();Room
One live room: a socket, its members and the shared state the room has confirmed.
class RoomNeeds
- 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
const room = await createBackend().rooms.create({ max: 4 });
room.send({ move: "e4" }, room.peers[0]);
room.transport(room.peers[0]); // "p2p" or "relay"deleteGameData
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.
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
await deleteGameData(env.BACKEND_DB, projectId);handleBackend
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.
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
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