> For the complete documentation index, see [llms.txt](https://docs.edgegap.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.edgegap.com/docs/sample-projects/web-games/threejs-rapier-arena.md).

# Three.js Rapier Arena

Make a browser (WebAssembly) game multiplayer: three.js and Rapier WASM physics, an authoritative Node.js server on Edgegap, and secure WebSockets (wss\://).

Learn how to make a browser (WebAssembly) game multiplayer, with three.js rendering, Rapier physics and an authoritative Node.js server.

![Two browser windows playing on the same server](https://raw.githubusercontent.com/edgegap/netcode-sample-threejs-rapier/main/docs/images/two-browser-windows.gif)

A three.js multiplayer arena where players roll balls and push crates around. Physics runs in [Rapier](https://rapier.rs), compiled to WebAssembly, in two places: on a Node.js game server that owns the authoritative simulation, and in the browser to predict the local player's movement so controls feel instant. The server runs at 30 ticks per second, takes only inputs from players, and sends every player a snapshot of all bodies each tick.

This sample shows:

* **Rapier WebAssembly physics** on the server and in the browser, with client-side prediction so controls feel instant.
* **One authoritative Node.js server** per session, at 30 ticks per second, taking only inputs from players.
* **Secure WebSockets** (`wss://`) from `https` pages, with TLS Upgrade on Edgegap.
* **A slim server image**, about 8 MB on top of Node.js, with the client hosted separately on a CDN.
* **Servers stop by themselves** after a few minutes without players.

{% hint style="info" %}
[\*\*Play Rapier Arena\*\*](https://edgegap.github.io/netcode-sample-threejs-rapier/) in your browser. See the [sample repository](https://github.com/edgegap/netcode-sample-threejs-rapier) for the client, the server, the Dockerfile and the GitHub workflows.
{% endhint %}

{% code collapsedlinecount="10" %}

```
Browser (static files on a CDN or any static host)
  three.js rendering + Rapier WASM (client-side prediction)
        |
        |  wss://<deployment host>:<external port>
        v
Edgegap deployment (slim Node.js container, near the players)
  Node.js 22 LTS game server, ws library, fixed 30 Hz tick
  Rapier WASM in Node.js = authoritative physics
```

{% endcode %}

The same structure works for Babylon.js, PlayCanvas, Phaser or your own engine: only the client's rendering code changes.

## ✔️ Preparation

Before we start, you will need:

* [Rapier Arena sample](https://github.com/edgegap/netcode-sample-threejs-rapier).
* [Node.js](https://nodejs.org/en/download) 22 or newer, to run the server and the client locally.

<details>

<summary><a href="https://www.docker.com/products/docker-desktop/">Install Docker Desktop (or Docker CLI)</a></summary>

* [Install Docker Desktop from the official source](https://www.docker.com/products/docker-desktop/) (no account required).
* Restart your computer after completing the installation.

</details>

☑️ Try the game locally first:

{% code collapsedlinecount="10" %}

```bash
git clone https://github.com/edgegap/netcode-sample-threejs-rapier.git
cd netcode-sample-threejs-rapier
npm install
npm run dev
```

{% endcode %}

`npm run dev` compiles the server, starts it on port `7777` and starts the client on port `5173`. Open <http://localhost:5173/?server=localhost:7777> in two browser windows. Roll with WASD or the arrow keys, jump with Space. Press P to switch prediction off and see the raw server delay on your own ball.

What's in the repository:

| Folder    | What it does                                                                                                      |
| --------- | ----------------------------------------------------------------------------------------------------------------- |
| `shared/` | Tick rate, arena, physics tuning, message types, and the Rapier setup used by both sides (`physics.ts`)           |
| `server/` | Node.js game server: Rapier world, fixed 30 Hz loop, WebSocket server, `/health`, graceful shutdown, `Dockerfile` |
| `client/` | Vite + three.js client: rendering, input, prediction (`prediction.ts`), connection and error messages (`net.ts`)  |

## 🌐 Why Browser Games Are Different

**Browsers can't use UDP.** Most game netcode sends UDP packets. Web pages can't open UDP sockets; real-time browser games use WebSockets (TCP) or WebTransport. This sample uses WebSockets, which every browser supports.

**Pages served over `https://` can only open `wss://` connections.** Your game page is almost always served over HTTPS (itch.io, GitHub Pages, any CDN). Chrome refuses `ws://` from such pages, so the game server must speak TLS, with a certificate that matches the address the client connects to. Edgegap does this for you with **TLS Upgrade**: it terminates TLS in front of your container with a valid certificate for the deployment's host name, so your Node.js server stays plain HTTP and WebSocket.

**Ship the client and the server separately.** A browser game is easy to bundle into one container that serves the page and runs the server. Don't: the client bundle (three.js, textures, audio, the WASM physics build) makes the image larger, so every deployment pulls more data and starts slower, and the extra files trip container scanners. Put the client on static hosting or a CDN, built once and cached worldwide. Put only the server in the image. In this sample the server image adds about 8 MB to the Node.js base image.

**Load WebAssembly without runtime downloads.** Rapier's `@dimforge/rapier3d-compat` package embeds its WASM binary in the JavaScript file. `await RAPIER.init()` compiles it from memory: no `.wasm` path to resolve inside the container, nothing fetched at startup, and the same code works in Node.js and the browser. The sample's server starts with networking disabled (`docker run --network none`), which proves it.

## ⚡ Deploy and Connect

### 1. Build the Server Image

☑️ To get started, you'll need to [create a free account with Edgegap](https://app.edgegap.com/auth/register). No credit card required.

☑️ Find your [Container Registry](https://docs.edgegap.com/learn/advanced-features/edgegap-container-registry.md) project name and credentials.

☑️ Sign in to Edgegap's Container Registry with Docker, using your registry project name, username and token:

{% tabs %}
{% tab title="Bash (Linux)" %}

```bash
project='<REGISTRY_PROJECT>';
username='robot$<REGISTRY_ORGANIZATION_ID>+client-push';
token='<REGISTRY_TOKEN>';

echo "$token" | docker login registry.edgegap.com -u "$username" --password-stdin 2>&1
```

{% endtab %}

{% tab title="PowerShell (Windows)" %}

```powershell
$project = '<REGISTRY_PROJECT>'
$username = 'robot$<REGISTRY_ORGANIZATION_ID>+client-push'
$token = '<REGISTRY_TOKEN>'

echo "$token" | docker login registry.edgegap.com -u "$username" --password-stdin 2>&1
```

{% endtab %}
{% endtabs %}

☑️ Build your server image from the repository root, so it can use the shared package, then push it:

{% code collapsedlinecount="10" %}

```bash
docker build --platform linux/amd64 --provenance=false --sbom=false -f server/Dockerfile -t registry.edgegap.com/<project>/rapier-arena-server:<tag> .
docker push registry.edgegap.com/<project>/rapier-arena-server:<tag>
```

{% endcode %}

* `--platform linux/amd64`: Edgegap runs Linux x86-64 containers, also when you build on an ARM Mac.
* `--provenance=false --sbom=false`: Docker Desktop otherwise wraps the image in a multi-manifest index with attestations.

{% hint style="danger" %}
:x: **DO NOT - overwrite existing tags or use `latest`  tag** - this will deploy an outdated cache.\
:white\_check\_mark: **DO - always increase your version tag** and deploy the new build.
{% endhint %}

☑️ Optionally, test your image locally first:

{% code collapsedlinecount="10" %}

```bash
docker run -p 7777:7777 registry.edgegap.com/<project>/rapier-arena-server:<tag>
```

{% endcode %}

The repository's `server-image.yml` workflow does the same on every push to `main` once you add the registry username and token as GitHub secrets.

What the Dockerfile does, and what it leaves out:

* **Multi-stage build.** The build stage installs all dependencies and compiles TypeScript. The runtime stage installs production dependencies only (`ws` and Rapier) and copies the compiled JavaScript.
* **No client files.** A `.dockerignore` allowlist sends Docker only the server, the shared package and the manifests: no client sources, no client build, no `node_modules`, no `.git`.
* **No unused Rapier files.** The server loads Rapier's ES module build, which embeds the WASM. The Dockerfile deletes the package's CommonJS copy, source maps and loose `.wasm` file (about 10 MB).
* **Pinned versions.** The base image is pinned by tag and digest (`node:22.23.4-alpine3.23@sha256:…`), and every npm dependency by exact version in `package.json` and `package-lock.json`.
* **Non-root user and PID 1.** The container runs as the `node` user, and `CMD ["node", "server/dist/index.js"]` runs Node.js directly, so the `SIGTERM` Edgegap sends on stop reaches the server instead of a shell or `npm`.

The result is 63.5 MB compressed (249 MB unpacked), of which about 8 MB is the game server and its dependencies on top of the official Node.js 22 Alpine image.

The server reads its port from `PORT` (default `7777`), answers `GET /health` with a small JSON status, closes every connection and exits on `SIGTERM`, and stops its own deployment after `EMPTY_SHUTDOWN_MINUTES` (default `5`) without players, including when nobody ever joins. That's what keeps a free-tier account from running an empty server until its maximum duration.

✅ You may now proceed to the next step.

### 2. Create an App Version

☑️ Create an application named `rapier-arena`, and a version with:

* image `registry.edgegap.com/<project>/rapier-arena-server`, with your tag,
* your Container Registry username and token,
* **Resources** of 0.25 vCPU and 512 MB memory,
* **Port** `7777`, **Protocol** `WS`, **TLS Upgrade** enabled, **Name** `gameport`,
* **Max duration** of 30 to 60 minutes, as a safety net for test deployments,
* optionally `EMPTY_SHUTDOWN_MINUTES` as an environment variable (default `5`).

{% hint style="warning" %}
**TLS Upgrade** lets browsers on `https` pages connect to `wss://<id>.pr.edgegap.net:<port>`. Edgegap terminates TLS with a certificate for the deployment's host name and forwards plain WebSocket traffic to port `7777`. Connect with the host name, not the IP, so the certificate matches.
{% endhint %}

{% hint style="info" %}
Free Tier is limited to 2 Applications, 2 versions, and 10 GB of Container Registry storage.
{% endhint %}

✅ You may now proceed to the next step.

### 3. Connect Directly

**Deploy.** Choose the version and **Deploy**. Edgegap places the server near the player IP addresses it's given (yours, from the dashboard). When the deployment is **Ready**, its page shows:

* **Host**: the deployment's host name, e.g. `a1b2c3d4e5f6.pr.edgegap.net`,
* **External port** of `gameport`: a port Edgegap picks at random for each deployment, e.g. `31504`.

**Connect.** Open the client (locally, or the hosted page from the next step), enter `<host>:<external port>` and press **Connect**, or open the page with `?server=<host>:<external port>`. The client builds `wss://a1b2c3d4e5f6.pr.edgegap.net:31504` from it. Send the same link to a friend on another network: both of you land on the same authoritative server.

🙌 Congratulations on your first Deployment on Edgegap!

Your own tools get the same details from the API: [`GET /v1/status/{request_id}`](https://docs.edgegap.com/docs/api.md) returns `fqdn` and `ports.gameport.external`, and `ports.gameport.link` holds both. A matchmaker returns them in its assignment.

{% hint style="danger" %}
**Do not integrate Edgegap API endpoints in game client, as your API token provides unlimited access to your account.** Players should get servers from a matchmaker or your own backend.
{% endhint %}

## 📦 Ship Your Game

The client is plain static files. Build it once:

{% code collapsedlinecount="10" %}

```bash
npm run build -w client
```

{% endcode %}

and upload `client/dist/` to any static host or CDN: GitHub Pages, Cloudflare Pages, Netlify, an S3 bucket behind CloudFront, or your own web server. Vite's `base: "./"` makes every path relative, so the build works from any folder.

The sample's `pages.yml` workflow publishes `client/dist/` to GitHub Pages on every push. Try it: [edgegap.github.io/netcode-sample-threejs-rapier](https://edgegap.github.io/netcode-sample-threejs-rapier/).

The client's own WASM (Rapier, for prediction) is embedded in the JavaScript bundle too, so there's no `.wasm` file to serve with the right MIME type. The bundle is about 4.9 MB (1.8 MB gzipped); most of it is Rapier. A CDN caches it after the first load.

{% hint style="success" %}
**Using a coding agent?** The [Edgegap MCP server](https://docs.edgegap.com/docs/mcp-integration.md) validates the Dockerfile and ports (`edgegap_validate_server_config`), creates the application and version (`edgegap_create_app`, `edgegap_create_app_version` with `protocol: "WS"` and `tls_upgrade: true`), deploys (`edgegap_deploy`) and returns the `wss://` address (`edgegap_wait_for_deployment`). Its `edgegap-browser-wasm` skill holds this whole guide for the agent: the shared physics module, prediction, the slim Dockerfile and the browser error messages.
{% endhint %}

## ⚠️ Troubleshooting

Browsers give scripts no details about a failed WebSocket: the `close` event only says code `1006`. The **browser console** (F12) has the real cause. The sample's client shows the matching fix under the **Connect** button. The messages below are copied from Chrome 157 and Edge 157 (Windows), with your address in place of the example's.

<details>

<summary>Mixed content: <code>ws://</code> from an <code>https://</code> page</summary>

{% code collapsedlinecount="10" %}

```
Mixed Content: The page at 'https://edgegap.github.io/netcode-sample-threejs-rapier/' was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint 'ws://example.com/'. This request has been blocked; this endpoint must be available over WSS.
```

{% endcode %}

`new WebSocket()` also throws:

{% code collapsedlinecount="10" %}

```
SecurityError: Failed to construct 'WebSocket': An insecure WebSocket connection may not be initiated from a page loaded over HTTPS.
```

{% endcode %}

Connect with `wss://<host>:<external port>` and enable TLS Upgrade on the app version's port. For a `ws://` address on your own network, Chrome instead logs `net::ERR_BLOCKED_BY_LOCAL_NETWORK_ACCESS_CHECKS`.

</details>

<details>

<summary>Certificate error: <code>net::ERR_CERT_COMMON_NAME_INVALID</code></summary>

{% code collapsedlinecount="10" %}

```
WebSocket connection to 'wss://203.0.113.10:31504/' failed: Error in connection establishment: net::ERR_CERT_COMMON_NAME_INVALID
```

{% endcode %}

The certificate doesn't match the address. The usual cause is connecting to the deployment's **IP address**: the certificate is issued for the host name. Use the deployment's **Host** (`…pr.edgegap.net`) with the same external port.

</details>

<details>

<summary>TLS not enabled: <code>net::ERR_SSL_PROTOCOL_ERROR</code></summary>

{% code collapsedlinecount="10" %}

```
WebSocket connection to 'wss://a1b2c3d4e5f6.pr.edgegap.net:31504/' failed: Error in connection establishment: net::ERR_SSL_PROTOCOL_ERROR
```

{% endcode %}

The port answered, but not with TLS. Edit the app version: port protocol `WS` with **TLS Upgrade** enabled, then start a new deployment.

</details>

<details>

<summary>Wrong port or connection refused: <code>net::ERR_CONNECTION_REFUSED</code></summary>

{% code collapsedlinecount="10" %}

```
WebSocket connection to 'wss://a1b2c3d4e5f6.pr.edgegap.net:7777/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
```

{% endcode %}

Use the **external** port of `gameport` from the deployment page, not the internal `7777`; it changes with every deployment. If the port is right, check the deployment is **Ready**, and read its container logs: the server prints `listening on 0.0.0.0:7777` once it accepts connections. A deployment that just became Ready can refuse connections for a few seconds; the sample's client retries five times, one second apart.

</details>

<details>

<summary>WASM initialization errors</summary>

With `@dimforge/rapier3d-compat`, the WASM binary is inside the JavaScript file, so the usual causes (wrong path, missing file in the container, a web server serving `.wasm` without the `application/wasm` MIME type, a blocked download) can't happen. If you switch to the non-compat `@dimforge/rapier3d` package to save bundle size, the browser must load a separate `.wasm` file: serve it from your static host with `Content-Type: application/wasm`, and in Node.js make sure the file is copied into the image.

Call `await RAPIER.init()` once, before creating a `World`. Creating one earlier fails with:

{% code collapsedlinecount="10" %}

```
TypeError: Cannot read properties of undefined (reading 'rawintegrationparameters_new')
```

{% endcode %}

</details>

<details>

<summary>Deployment keeps running after everyone left</summary>

The server stops its own deployment after `EMPTY_SHUTDOWN_MINUTES` without players. Check the container logs for `no players: stopping in …` and `self-stop request: HTTP 200`. The server also pings every WebSocket every 15 seconds and drops connections that stop answering, so a closed laptop doesn't keep the server busy.

</details>

## 🚀 Going Further

* **Matchmaking.** Replace the server field with a **Find match** button: the [Edgegap matchmaker](https://docs.edgegap.com/web-games/matchmaking.md) groups players, deploys the server and returns the same host and external port. The [Three.js Minigames](https://docs.edgegap.com/docs/sample-projects/web-games/threejs-minigames.md) sample shows it end to end.
* **Region selection.** Measure latency from the browser to Edgegap [ping beacons](https://docs.edgegap.com/learn/orchestration/ping-beacons.md) and send the results to the matchmaker so each match starts where its players get the lowest ping.
* **Other samples.** [Orb Arena](https://github.com/edgegap/netcode-sample-fal-orb-arena) takes an AI-generated three.js game online with the same Node.js server pattern. Unity WebGL and Godot web variants of this guide are planned, using the same structure.
* **Start from scratch.** [Web Games (JS/TS) - Getting Started](https://docs.edgegap.com/web-games.md) deploys a minimal Node.js WebSocket server in a few minutes.
