# Self-hosting

> Run the open-source SyncVerse server yourself and point the Unity SDK at it.

Source: https://syncverse.aqaddoura.com/docs/self-hosting

## Run the server

The server is a Node.js 18+ process (TypeScript, MIT license). One process serves the lobby on `/master` and rooms on `/rooms/<id>`.

```bash
git clone https://github.com/simple-yet-efficient/syncverse-server
cd syncverse-server
npm install          # uWebSockets.js is downloaded from GitHub
npm run build
PORT=9000 PUBLIC_URL=ws://localhost:9000 npm start
```

> The `syncverse-server` repository is private for now, so the clone needs access: ask Simple Yet Efficient for it. The same applies to the `CONTRACT.md` link under [Protocol](https://syncverse.aqaddoura.com/docs/protocol).

Check it with `GET /health`, which returns JSON with the room and player counts. `GET /metrics` serves the same numbers in Prometheus text format.

## Point Unity at it

In the `SyncVerseSettings` asset set **Mode** to **Self-hosted** and **Server URL** to your server, or call `master.Connect("ws://localhost:9000")`. The App ID and tokens are ignored by a self-hosted server.

## Settings

Everything is optional and read from the environment (or a `.env` file).

| Variable | Default | Meaning |
| --- | --- | --- |
| `PORT` | 9000 | Listen port |
| `BIND_HOST` | 0.0.0.0 | Interface to bind |
| `PUBLIC_URL` | ws://localhost:<PORT> | Base URL clients use; room URLs are built from it |
| `MAX_PAYLOAD` | 65536 | Largest frame in bytes |
| `MAX_ROOMS` | 1000 | Rooms on this server; CreateRoom answers ServerFull beyond |
| `MAX_PLAYERS_PER_ROOM` | 64 | Cap on the requested maxPlayers |
| `EMPTY_ROOM_TTL_MS` | 30000 | An empty room is removed after this long |
| `BUFFER_LOG_LIMIT` | 1000 | Buffered spawns and RPCs kept per room |
| `MAX_FRAMES_PER_SEC` | 0 | Frames per second per room socket; 0 means no limit |
| `TRUST_PROXY` | false | Take the client IP from X-Forwarded-For |

## TLS with a reverse proxy

The server speaks plain `ws://`. Put a reverse proxy in front for `wss://`, which WebGL builds on https pages and mobile platforms need. With Caddy, certificates are automatic:

**Caddyfile**

```text
game.example.com {
    reverse_proxy localhost:9000
}
```

Then set `PUBLIC_URL=wss://game.example.com` and `TRUST_PROXY=true`, and use `wss://game.example.com` in Unity.

### Without TLS

> Plain `ws://` works in the editor and desktop builds. Android needs cleartext traffic allowed and iOS needs an App Transport Security exception, so use TLS for anything you ship.

## Same code as the cloud

SyncVerse Cloud runs this server on its game nodes, with the lobby on separate gateways. Self-hosting gives you one process with everything in it; see [Introduction](https://syncverse.aqaddoura.com/docs/introduction) for what the cloud adds.
