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:
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:
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 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/
├── 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
--client protocol:server/+client/(nodemo/).--client api: noserver/folder:Cargo.tomlandsrc/main.rsat the root.--existing <game>(Bevy): the demo goes intonet_backend_demo/beside the game (its own workspace), and the installer prints the two lines for the game'sCargo.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.
| Module | Buttons | Pushes in the log |
|---|---|---|
| auth | register / login / refresh / logout | |
| storage | save / load / list | |
| chat | join / send / history / presence, edit my last message / typing / read marker | chat.message, chat.presence, chat.edited, chat.typing, chat.read |
| leaderboards | submit / top / around me | |
| notifications | list / mark all read | notify.new |
| friends | my code / add by code / list / accept | friends.presence |
| groups | create / invite / list | |
| lobbies | create / join by code / ready / leave | lobby.member, lobby.changed |
| matchmaking | queues / queue / cancel | match.found, match.expired |
| files | upload / 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 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
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):
[database]
url = "sqlite::memory:"
migrate_on_start = true
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):
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:
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… | Installer | Use |
|---|---|---|
| a Rust app (tool, bot, CLI, other engine) | --client rust | net_backend_client |
| a Bevy game | --client bevy | bevy_net_backend + net_backend_protocol |
| your own Rust networking | --client protocol | net_backend_protocol + any HTTP / WebSocket library |
| not Rust (C#, GDScript, JavaScript, Python, …) | --client api | the 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 →