createRoom()
Admit a room with the Ball2D service, start its host and resolve with the room.
createRoom() validates your settings, asks the service to admit the room, loads the engine, connects to signaling and resolves once the service has confirmed your process as the host. It rejects if any step fails, and cleans up everything it started.
Node.js
createRoom()
createRoom(config: NodeRoomConfig, creation?: CreateRoomOptions): Promise<NodeRoom>The whole startup must finish within 15 seconds. A missing or malformed API key is reported before any other setting, and before the engine or network are touched.
| Failure | Rejects with |
|---|---|
Missing or malformed apiKey | TypeError: A valid API key is required. |
serviceOrigin is not HTTPS and not your own machine | TypeError |
| An invalid room setting | Error naming the setting |
| The service refuses the room | RoomAdmissionError |
| Startup takes longer than 15 seconds | DOMException named TimeoutError |
creation.signal aborts during startup | The signal's reason |
Parameters
CreateRoomOptions{ signal } to cancel startup.Returns
Promise<NodeRoom>Resolves with the running room once host authority is confirmed.
import {
createRoom,
RoomAdmissionError,
} from 'ball2d/node';
try {
const room = await createRoom({
apiKey: process.env.BALL2D_API_KEY,
roomName: 'Evening football',
noPlayer: true,
maxPlayers: 12,
public: true,
});
console.log(room.roomLink);
} catch (error) {
if (error instanceof RoomAdmissionError) {
console.error(error.status, error.message);
} else throw error;
}const room = await createRoom(config, {
signal: AbortSignal.timeout(10_000),
});NodeRoomConfig
Import fromball2d/node
interface NodeRoomConfig extends RoomConfigstringhttps://ball2d.com. Must be HTTPS, except http://localhost, http://127.0.0.1 and http://[::1] for local development, and must not include a path, query or credentials. A custom origin must serve a matching Ball2D deployment.stringrequiredNodeRoom
Import fromball2d/node
The value createRoom() resolves with in Node.js: every Room property, method and event, plus closed.
closed
room.closed: Promise<void>Resolves after the room has closed and your process has released everything the host used: the simulation, peer connections and the service authorization. Await it before exiting so the room is not left waiting for its connection to time out.
It rejects only if closing the peer transport itself fails.
room.close();
await room.closed;
process.exit(0);Settings
RoomConfig
Import fromball2d
Settings shared by both entries. Unknown keys are rejected, so a typo fails at startup instead of being ignored.
interface RoomConfigbooleanfalse (the default), the host takes a player slot as player 0, an admin named by playerName. Set true for a room run entirely by code. sendChat() needs the host player.stringHost. Used only when noPlayer is false.stringHeadless Room.number12.stringsetPassword().booleanfalse: the room is reachable only through its link.stringsetDefaultStadium() to pick a bundled stadium by name.RoomGeoCreateRoomOptions
Import fromball2dball2d/node
interface CreateRoomOptionsAbortSignalRoomGeo
Not exported by name; reach it through the values that return it.
interface RoomGeoPublic discovery location; never a routing or authorization claim.
stringrequiredDE. Case-insensitive.numberrequirednumberrequiredBrowser
createRoom() in the browser
createRoom(config?: RoomConfig, options?: CreateRoomOptions): Promise<Room>The browser entry hosts a room from a page served by a Ball2D deployment. It uses the service, engine and stadium files of the page's own origin, follows the browser admission policy and never takes an API key. See Browser entry.
Parameters
RoomConfigCreateRoomOptions{ signal } to cancel startup.Returns
Promise<Room>Resolves with the running room.
import { createRoom } from 'ball2d';
const room = await createRoom({
roomName: 'Practice',
});