net_backend

Get started · from zero to a running server

Start your server.

Three ways in: the installer writes a whole project for you; or you add the crates to a project of your own; or you run the server image with Docker, no Rust needed. Rust from rustup.rs, 1.95 or newer.

1. The installer

net-backend new asks a few questions (or takes flags) and writes a game backend that runs at once: the server with the modules you pick, a client for your kind of game, a demo app with buttons, and Docker files for your database. The same in PowerShell, cmd, bash and zsh:

shell
cargo install net_backend
net-backend new mygame
cd mygame
cargo run              # the server on http://127.0.0.1:8080, on SQLite
cargo run -p demo      # in a second terminal: the demo window, a panel per module

The questions, in order: how your game talks to the server (Rust client, Bevy plugin, your own client, pure API), the database (SQLite, PostgreSQL, MySQL), the server modules (all ticked except oauth, which needs a provider such as a Google OAuth client set up first), a demo app (Rust and Bevy), for Bevy where the demo goes, and whether to start everything at once. Try them on the home page.

Every question is a flag; with any flag (or without a terminal, e.g. CI) nothing is asked and the defaults fill the rest; -y / --yes takes every default. At the end the installer prints the one-line command that gives the same project:

net-backend new mygame -y
Created `mygame` (net_backend 0.2.0).

Next:
  cd mygame
  cargo run              # the server on http://127.0.0.1:8080
  cargo run -p demo      # the demo, in a second terminal
  (the demo with Steam: cargo run -p demo --features steam; see README.md)

The same setup without questions:
  net-backend new mygame --client rust --db sqlite --modules auth,storage,chat,leaderboards,notifications,friends,groups,lobbies,matchmaking,files --demo

README.md in the project describes the rest.

Every flag

net-backend --help
net-backend 0.2.0: the net_backend installer. The projects use net_backend_server,
net_backend_protocol and net_backend_client 0.2.0.

Usage:
  net-backend new [<name>] [options]   a new project; asks what is missing (in a terminal)
  net-backend new-server <name> [options]
                                       the same as `new <name> --client api`: the server alone
  net-backend new-client <name>        a Rust client alone (for a server that exists)
  net-backend --help | --version

Options of `new` (every question is a flag; with any flag nothing is asked):
  --client rust|bevy|protocol|api      how the game talks to the server (default rust):
                                         rust      server + protocol + net_backend_client
                                         bevy      server + protocol + bevy_net_backend
                                         protocol  server + protocol, your HTTP / WebSocket library
                                         api       the server only (any language: API.md)
  --db sqlite|postgres|mysql           the database (default sqlite)
  --modules <list>|none                the server modules, comma-separated; each one adds auth:
                                         auth, storage, chat, leaderboards, notifications,
                                         friends, groups, oauth, lobbies, matchmaking, files
                                       (default: all but oauth, which needs a provider set up)
  --demo | --no-demo                   a demo app with buttons and a log (rust, bevy; default yes)
  --existing <path>                    bevy: put the demo next to this existing game, in
                                       net_backend_demo/ (the game's files are not changed)
  --run                                build, start the server in a new terminal window, then
                                       the demo (or the client) in this one
  -y, --yes                            no questions: the defaults for everything not given
  --no-color                           plain text without colors (any command; NO_COLOR does
                                       the same)

<name> is the folder to create; a path works too, its last part is the project name (a-z, 0-9,
`-` and `_`, starting with a letter). An existing folder must be empty.

Examples (the same in PowerShell, cmd, bash and zsh):
  net-backend new mygame
  net-backend new mygame --client rust --db sqlite --modules auth,storage,chat --demo --run
  net-backend new mygame --client bevy --db postgres --modules chat,friends,lobbies --no-demo
  net-backend new mygame --client rust --db sqlite --modules storage,leaderboards,oauth
  net-backend new mygame --client api --db mysql --modules none

More: https://net-backend.com

What it writes

mygame/
mygame/
├── Cargo.toml                the workspace; `cargo run` = the server
├── README.md                 how to run, deploy and extend it, and the one-line command
├── config.toml               the server's development configuration
├── config.docker.toml        SQLite only: the configuration inside the Docker image
├── Dockerfile                the server in a small non-root Linux image
├── compose.yaml              SQLite: the image on 127.0.0.1:8080; PostgreSQL / MySQL: production with Caddy
├── .gitignore, .dockerignore (with the files module, .gitignore also lists the players' files folder /files)
├── server/
│   ├── Cargo.toml            net_backend_server with the database's and the modules' features, tokio
│   └── src/main.rs
├── client/
│   ├── Cargo.toml            rust: net_backend_client · bevy: bevy_net_backend + net_backend_protocol · protocol: net_backend_protocol
│   └── src/main.rs
└── demo/                     rust and bevy, with --demo
    ├── Cargo.toml            rust: egui · bevy: Bevy
    └── src/main.rs, src/steam.rs, src/invite.rs
the existing Bevy game's Cargo.toml
bevy_net_backend = { version = "0.2.0", features = ["http", "json", "ws"] }
net_backend_protocol = { version = "0.2.0", features = ["bevy_net_backend"] }

The demos

A panel per module the server reports (only the ones you picked) and a log: -> what the client sent, <- what came back, << what the server pushed; also printed in the terminal, while the server's terminal shows the other side.

ModuleButtonsPushes in the log
authregister / login / refresh / logout
storagesave / load / list
chatjoin / send / history / presence, edit my last message / typing / read markerchat.message, chat.presence, chat.edited, chat.typing, chat.read
leaderboardssubmit / top / around me
notificationslist / mark all readnotify.new
friendsmy code / add by code / list / acceptfriends.presence
groupscreate / invite / list
lobbiescreate / join by code / ready / leavelobby.member, lobby.changed
matchmakingqueues / queue / cancelmatch.found, match.expired
filesupload / list / download

Two players: start the demo twice with two accounts (cargo run -p demo -- player2@example.com), then add each other by friend code, join a lobby by its code, or queue both in duel. With Steam: cargo run -p demo --features steam (Steam).

--run builds, opens the server in a new terminal window (Windows console, macOS Terminal, the first Linux terminal found), waits until /v1/info answers, then starts the demo. Without a window (SSH, no display) the server runs in the same terminal. The first Bevy build takes 5 to 10 minutes, the Rust demo about 1 to 2 minutes.

2. Add the crates yourself

In your own project, pick one of the first three lines (the database and the modules), then add tokio and serde:

cargo add
cargo add net_backend_server                                                  # PostgreSQL (the default)
cargo add net_backend_server --no-default-features --features sqlite          # or SQLite (MySQL / MariaDB: --features mysql)
cargo add net_backend_server --no-default-features --features sqlite,storage,chat   # or SQLite with storage + chat
cargo add tokio --features rt-multi-thread,macros
cargo add serde --features derive                                             # for your own request types
src/main.rs
use net_backend_server::axum::{routing::post, Json};
use net_backend_server::{ApiJson, AppError, Config, NetBackendServer};
use serde::Deserialize;

#[derive(Deserialize)]
struct Craft {
    item: String,
}

async fn craft(ApiJson(body): ApiJson<Craft>) -> Result<Json<String>, AppError> {
    if body.item.is_empty() {
        return Err(AppError::bad_request("item is empty"));
    }
    Ok(Json(format!("crafted {}", body.item)))
}

#[tokio::main]
async fn main() -> Result<(), net_backend_server::Error> {
    let config = Config::load()?;                    // NBS_CONFIG / config.toml + NBS__* variables
    NetBackendServer::new(config)
        .route("/v1/game/craft", post(craft))      // plain axum handlers
        .run()                                     // the command line; `serve` by default
        .await
}

With the sqlite feature, a config.toml in the folder cargo run runs in names the database (an in-memory one here; sqlite:game.db is a file, created on the first start):

config.toml
[database]
url = "sqlite::memory:"
migrate_on_start = true
shell
cargo run

http://127.0.0.1:8080/v1/info in a browser (or curl.exe on Windows, curl elsewhere) answers {"protocol":1,"min_protocol":1,"modules":[]}. Modules register like routes, one .module(…) each, in the order Auth, Storage, Chat, Leaderboards, Notifications, Friends, Groups, OAuth, Lobbies, Matchmaking, Files: see the modules and the generated main.rs on the home page.

3. Docker, no Rust needed

The reference server with every module, for linux/amd64 and linux/arm64. The same command in PowerShell, cmd, bash and zsh (Docker Desktop on Windows / macOS, Docker Engine on Linux):

the trial
docker run --rm -p 127.0.0.1:8080:8080 ghcr.io/warmar94/net_backend_server:0.2

Then http://127.0.0.1:8080/v1/info answers {"protocol":1,"min_protocol":1,"modules":["auth","chat","files","friends","groups","leaderboards","lobbies","matchmaking","notifications","oauth","storage"]}. Keep the data and create an administrator:

shell
docker run -d --name nbs -p 127.0.0.1:8080:8080 -v nbs-data:/data ghcr.io/warmar94/net_backend_server:0.2
docker exec nbs net-backend-server user:create admin@example.com --admin

The trial is plain HTTP with open registration, for your own computer: the port is published on 127.0.0.1 only. A server for players uses the production Compose file (HTTPS through Caddy).

Connect a client

Every path talks to the same server, in this order:

Your client is…InstallerUse
a Rust app (tool, bot, CLI, other engine)--client rustnet_backend_client
a Bevy game--client bevybevy_net_backend + net_backend_protocol
your own Rust networking--client protocolnet_backend_protocol + any HTTP / WebSocket library
not Rust (C#, GDScript, JavaScript, Python, …)--client apithe API reference, /v1/openapi.json and /v1/asyncapi.json

Deploy

Production with Docker Compose in three commands (PostgreSQL 16 or MySQL 8.4, Caddy with HTTPS / WSS, secrets generated on the first start), or systemd. A project from the installer brings its own: docker compose up -d --build. Deploying a server →