Room
The controller createRoom() resolves with; identity, lifecycle and the method groups.
A Room is your handle on one running room. It exposes read-only identity, synchronous reads, queued commands and event properties. In Node.js, createRoom() resolves with a NodeRoom, which adds closed.
Import fromball2d
| Group | Methods |
|---|---|
| Players and teams | getPlayerList getPlayer setPlayerTeam setPlayerAdmin setPlayerAvatar reorderPlayers setTeamsLock setTeamColors |
| Moderation | setPlayerMuted kickPlayer clearBan clearBans setPassword setRequireVerification |
| Chat | sendChat sendAnnouncement |
| Matches | startGame stopGame pauseGame setScoreLimit setTimeLimit setKickRateLimit getScores getState getBallPosition |
| Stadiums | setDefaultStadium setCustomStadium |
| Physics | CollisionFlags getDiscCount getDiscProperties setDiscProperties getPlayerDiscProperties setPlayerDiscProperties |
| Recording | startRecording stopRecording lastRecording |
Identity
roomId
room.roomId: stringThe room's identifier: ten lowercase hexadecimal characters, such as 3f9a0c41be.
console.log(room.roomId); // "3f9a0c41be"roomLink
room.roomLink: stringThe invitation URL players open to join, https://ball2d.com/r/<roomId> for rooms on the default service. It is available as soon as createRoom() resolves.
console.log(`Join at ${room.roomLink}`);roomName
room.roomName: stringThe roomName you passed to createRoom(), exactly as given.
Lifecycle
signal
room.signal: AbortSignalAn AbortSignal that aborts when the room closes, for any reason: your close() call, a lost connection, a revoked or expired key. Pass it to timers, fetches and loops that should stop with the room.
const timer = setInterval(
announceScore,
60_000,
);
room.signal.addEventListener('abort', () =>
clearInterval(timer),
);
// Or stop a fetch when the room ends:
await fetch(url, { signal: room.signal });close()
room.close(): voidCloses the room. Calling it again does nothing. In order, it:
- Rejects every queued command with
Room is closed. - Aborts
signal. - Finalizes an active recording and delivers it to
onRecordingCompletewith the reasonRoom closed. - Leaves the service, which tells players the host has left.
close() returns immediately. In Node.js, await closed for cleanup to finish. It does not wait for every player to receive the shutdown.
After closing, commands reject, events stop (except the final onRecordingComplete), getPlayerList() returns [] and the other reads return null or 0.
for (const signal of ['SIGINT', 'SIGTERM']) {
process.once(signal, async () => {
room.close();
await room.closed;
process.exit(0);
});
}