How to host a game server for browser games
Net Forge, the open-source multiplayer browser game engine built for AI coding agents, is published on npm under @netforge with source at github.com/netnetdev/net-forge.
A browser game server is a long-running process that accepts WebSocket connections, keeps each room's world in memory, steps it at a fixed tick and sends snapshots back. Browsers cannot open raw sockets, so the server must speak WebSockets (or WebTransport) over TLS when the page is served over HTTPS, and every player in a room must reach the same process. Net Forge's local host does all of this on one port for your own computer and network:
npm create netforge@latest my-game -- --template arena --yes
cd my-game
npm run play -- --host 0.0.0.0 --no-openWhat a game server needs
| Need | Why | How the Net Forge local host does it |
|---|---|---|
| WebSockets | Browsers can only open WebSocket, WebRTC or WebTransport connections | A Colyseus room over its WebSocket transport, on the same port as the page |
| An authoritative loop | The server decides outcomes, clients send inputs | Fixed tick (30 or 60 Hz), one input queue per player |
| Snapshots per player | Each player needs what is near them, as cheaply as possible | Area of interest per template, delta snapshots at 20 Hz |
| Room placement | Everyone in a room must reach the same process | One process holds every room (at most 8) |
| TLS | An HTTPS page cannot open an unencrypted WebSocket | Not included: the local host serves plain HTTP on your network |
| Persistence | Saved progress survives a restart | Not included: progress lives in memory |
| Lag compensation and replay | Fair hitscans over the internet, auditable runs | Not included locally; the hosted Net Forge server has both |
1. Run the local host
npm run play builds the client and starts the host. The flags that matter for hosting:
npm run play -- --port 8080 --host 0.0.0.0 --no-open--port sets the one port for the page and the room, --host 0.0.0.0 listens on every interface instead of only 127.0.0.1, and --no-open prints the link without opening a browser. The client finds its room from the server parameter in the printed link.
2. Decide where production rooms run
For a game anyone on the internet can join, you need TLS, rooms placed close to players, restarts that do not drop everyone and saved progress. For Net Forge games, the hosted studio at forge.netnet.capital runs those room servers; see Deploy and host your game for how a local game reaches it. If you build your own server stack, the options below are the established ones.
3. Load test before you ship
Measure the server cost per tick before choosing machine sizes. npm test prints it for your game:
npm test -- --bots 4 --minutes 5For scale, the templates' own playtests on an Apple M2 Pro with Node 22: the survivor template holds 2,000 enemies with 4 bots at 2.22 ms per tick on average (p99 5.21 ms), against a 33 ms budget at 30 Hz. The arena shooter costs 0.10 ms per tick at 60 Hz with 4 bots. One process can run many rooms of a cheap game, and few of an expensive one.
How it works
The simulation is a plain function of the world and that tick's inputs: step(world, inputs) never reads a clock. The host's loop calls it on a timer, decodes input packets into each player's queue, and encodes one snapshot per player against the tick that player last acknowledged. Because the same module runs in the browser for prediction, the server and the client never disagree about the rules. See Simulation and ECS and Netcode and prediction.
Alternatives
- Colyseus is an MIT-licensed Node framework for room-based game servers. Its deployment docs cover PM2, Nginx, Apache and Docker, and Colyseus Cloud is its managed hosting. Net Forge's local host is itself built on a Colyseus room.
- Nakama is an open-source game server with accounts, matchmaking, storage and authoritative multiplayer, which you can run yourself.
- PartyKit runs real-time rooms on a hosted platform and handles the infrastructure and scaling for you.
- Socket.IO gives you a WebSocket server with rooms and fallbacks, on which you write the game loop yourself.
FAQ
Do browser games need a dedicated server?
Real-time multiplayer games with fair results do: a server that runs the game and decides outcomes. Turn-based games can use a lighter server that validates moves. Peer-to-peer over WebRTC is possible but leaves one player's browser in charge.
Can I host a WebSocket game server on serverless functions?
Not the classic kind, because a room's world must stay in memory in one process for the whole match. Use long-running processes, containers, or a platform built for stateful rooms.
What tick rate should a browser game server use?
30 Hz suits most action games and is what most Net Forge templates use. Fast shooters use 60 Hz, as the arena template does. Snapshots can go out less often than the tick (the local host sends 20 a second) because clients interpolate.
Why does my HTTPS page fail to connect to my game server?
Browsers block an unencrypted ws:// connection from an https:// page. The server needs TLS (wss://), usually from a reverse proxy in front of it.
How many players can one server handle?
It depends on the game's cost per tick, which npm test reports. A room of the arena template costs about 0.1 ms per tick on a laptop core, so CPU is rarely the first limit for small rooms; bandwidth and memory per room usually come first.