|
| 1 | +Get a SpacetimeDB C++ app running in under 5 minutes. |
| 2 | + |
| 3 | +## Prerequisites |
| 4 | + |
| 5 | +- [SpacetimeDB CLI](https://spacetimedb.com/install) installed |
| 6 | +- [Emscripten SDK](https://emscripten.org/docs/getting_started/downloads.html) 4.0.21+ installed |
| 7 | +- CMake 3.20+ and a make/ninja backend |
| 8 | +- C++20 toolchain (host) — build targets WASM via Emscripten |
| 9 | + |
| 10 | +After installing the SDK, run the appropriate `emsdk_env` script (PowerShell or Bash) so `emcc` and the CMake toolchain file are available on `PATH`. |
| 11 | + |
| 12 | +Install the [SpacetimeDB CLI](https://spacetimedb.com/install) before continuing. |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## Install Emscripten |
| 17 | + |
| 18 | +Use the official SDK (see [Emscripten downloads](https://emscripten.org/docs/getting_started/downloads.html)) and activate the environment so `emcc` and the CMake toolchain file are on PATH. We recommend Emscripten 4.0.21+. |
| 19 | + |
| 20 | +```bash |
| 21 | +# From your emsdk directory (after downloading/cloning) |
| 22 | +# Windows PowerShell |
| 23 | +./emsdk install 4.0.21 |
| 24 | +./emsdk activate 4.0.21 |
| 25 | +./emsdk_env.ps1 |
| 26 | + |
| 27 | +# macOS/Linux |
| 28 | +./emsdk install 4.0.21 |
| 29 | +./emsdk activate 4.0.21 |
| 30 | +source ./emsdk_env.sh |
| 31 | +``` |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +## Create your project |
| 36 | + |
| 37 | +Use the CLI-managed workflow with `spacetime build`, which wraps CMake + `emcc` for you, starts the local server, builds/publishes your module, and generates client bindings. |
| 38 | + |
| 39 | +```bash |
| 40 | +spacetime dev --template basic-cpp |
| 41 | +``` |
| 42 | + |
| 43 | + |
| 44 | +Need manual control? You can still drive CMake+emcc directly (see `spacetimedb/CMakeLists.txt`), but the recommended path is `spacetime build`/`spacetime dev`. |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +Server code lives in the `spacetimedb` folder; the template uses CMake and the SpacetimeDB C++ SDK. |
| 51 | + |
| 52 | + |
| 53 | +``` |
| 54 | +my-spacetime-app/ |
| 55 | +├── spacetimedb/ # Your C++ module |
| 56 | +│ ├── CMakeLists.txt |
| 57 | +│ └── src/ |
| 58 | +│ └── lib.cpp # Server-side logic |
| 59 | +├── Cargo.toml |
| 60 | +└── src/ |
| 61 | + └── main.rs # Rust client application |
| 62 | +``` |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +## Understand tables and reducers |
| 67 | + |
| 68 | +The template includes a `Person` table and two reducers: `add` to insert, `say_hello` to iterate and log. |
| 69 | + |
| 70 | +```cpp |
| 71 | +#include "spacetimedb.h" |
| 72 | +using namespace SpacetimeDB; |
| 73 | + |
| 74 | +struct Person { std::string name; }; |
| 75 | +SPACETIMEDB_STRUCT(Person, name) |
| 76 | +SPACETIMEDB_TABLE(Person, person, Public) |
| 77 | + |
| 78 | +SPACETIMEDB_REDUCER(add, ReducerContext ctx, std::string name) { |
| 79 | + ctx.db[person].insert(Person{name}); |
| 80 | + return Ok(); |
| 81 | +} |
| 82 | + |
| 83 | +SPACETIMEDB_REDUCER(say_hello, ReducerContext ctx) { |
| 84 | + for (const auto& person : ctx.db[person]) { |
| 85 | + LOG_INFO("Hello, " + person.name + "!"); |
| 86 | + } |
| 87 | + LOG_INFO("Hello, World!"); |
| 88 | + return Ok(); |
| 89 | +} |
| 90 | +``` |
| 91 | +
|
| 92 | +
|
| 93 | +
|
| 94 | +## Test with the CLI |
| 95 | +
|
| 96 | +Open a new terminal and navigate to your project directory. Then call reducers and inspect data right from the CLI. |
| 97 | +
|
| 98 | +```bash |
| 99 | +cd my-spacetime-app |
| 100 | +
|
| 101 | +# Insert a person |
| 102 | +spacetime call add Alice |
| 103 | +
|
| 104 | +# Query the person table |
| 105 | +spacetime sql "SELECT * FROM person" |
| 106 | +
|
| 107 | +# Call say_hello to greet everyone |
| 108 | +spacetime call say_hello |
| 109 | +
|
| 110 | +# View the module logs |
| 111 | +spacetime logs |
| 112 | +``` |
| 113 | + |
| 114 | +## Notes |
| 115 | + |
| 116 | +- To use a local SDK clone instead of the fetched archive, set `SPACETIMEDB_CPP_SDK_DIR` before running `spacetime dev`/`spacetime build`. |
| 117 | +- The template builds to WebAssembly with exceptions disabled (`-fno-exceptions`). |
| 118 | +- If `emcc` is not found, re-run the appropriate `emsdk_env` script to populate environment variables. |
0 commit comments