Multiplayer games
A project can have its own game server for real-time multiplayer: rooms that players join, state that every player sees change live, chat, and matchmaking. It runs on Colyseus 0.18, beside your site and your data API and apart from them, so a busy game never slows your data API down.
Start from the template #
New project → Multiplayer game. Turn on Built-in sign-in if players should play under their accounts. You get:
- A lobby on the home page. Rooms show up and fill up live. You can create a room, join one, or press Quick play.
- An arena: up to 8 players move around a board and grab coins. The server decides where everyone is and who scored, so nobody can cheat by sending a fake position.
- A scoreboard and chat in every room.
- Keyboard controls (arrows or WASD) and a touch pad on phones.
Open the preview and play it in two tabs (or on your phone and your computer) to see it work.
How it fits together #
| Where | What it is |
|---|---|
server/index.js | Lists the rooms players can join. |
server/ArenaRoom.js | A room: what happens when players join, send input and leave. |
server/state.js | The state every player in a room sees, kept in sync automatically. |
src/game.js | Written by Devmachinist: the address of your game server and gameClient(). Don't edit it. |
src/components/Lobby.jsx, Arena.jsx | The pages players see, ordinary React and Tailwind. |
Your site talks to the game server through gameClient():
import { gameClient } from '../game.js'
const room = await gameClient().joinOrCreate('arena', { name: 'Ann' })
room.send('input', { x: 1, y: 0 })
room.onMessage('chat', (m) => console.log(m.from, m.text))Dev and live #
Each project has two game servers:
- Dev runs the code in
server/as you edit it, while the preview is open. Saving a file inserver/restarts it, so rooms start fresh. - Live runs the code from your last publish. Publish again to put changes to
server/live. Players in a room are disconnected when it restarts.
A game server starts when the first player connects and stops after everyone has left for 10 minutes. Keep nothing in a room that you can't lose.
More → Publish → Game server shows both servers, how many players are connected and the server's recent output (errors and console.log lines), with Start, Restart and Stop.
Players who sign in #
If your site has sign-in, a signed-in player is recognised in every room. Devmachinist checks their sign-in before they reach your game server and hands your room who they are. In onAuth read it with playerFrom(context) (in server/player.js): their id, email, name and role. Guests have none. A player can't pretend to be someone else, because only Devmachinist can set it.
Rules for the game server #
- It's plain JavaScript (ES modules). Rooms import from
server/,shared/(code both sides use) and installed packages. - It has no internet access, so it can't call outside services or your data API. Keep scores in the room. To save a result, have the page save it with your data client after the game.
- It has about 256 MB of memory. Each project can run a few game servers at once.
- The server owns the truth. Players send what they want to do, and the room decides what happens.
With the AI #
The AI chat knows how the game server works (get_context {topic: "game"}) and can write rooms in server/. After a change it reads game_status to see whether the server started and what it printed, and it can test the rooms with game_test. That tool sends two or more test players onto the game server: they join rooms, invite each other and check that every step happens as planned, such as a boss not coming until everyone is in place. Agents get the same through the MCP server.