Tutorial: a guestbook
Build Guestbook: one book anyone can sign. New notes show up for everyone at once, a note's
writer can take it back, and the whole book downloads as text. It runs on a server. The finished
app is in examples/guestbook.
You need Rust (through rustup), Node 22, and the soda command (Getting started).
1. Make the app#
--plain starts from a shared checklist that stores its own state in SQLite. (Without it you get
the event-sourced board: see Events.) Open the address soda dev prints in two
windows and add an item: it shows in both. Edit the words in src/routes/layout.rs and save. Both
windows update in about a second, and the list is still there.
| Path | What |
|---|---|
src/list.rs | The app's state: the list, in SQLite |
migrations/ | The database schema, one file per change, run at startup |
src/routes/ | The pages: a folder is an address, page.rs renders it, actions.rs handles its forms |
src/components.rs | Shared #[component]s |
src/main.rs | soda_server::app!, naming the state |
web/app.html, web/app.css | The page shell, and the design tokens |
tests/e2e.mjs | End-to-end tests in real browsers |
Delete what the guestbook doesn't need: src/list.rs, src/components.rs, src/routes/items/,
migrations/0001_items.sql, and web/components/. Delete data/
too, for an empty database.
2. Store the entries#
The app's state is one type, made at startup and shared by every request (so it's
Send + Sync). The guestbook's state is a SQLite pool in the app's data folder:
soda_server::data(), which is data/ in the folder the app was built in, or SODA_DATA.
migrations/0001_entries.sql:
(
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
text TEXT NOT NULL,
at INTEGER NOT NULL,
by_session TEXT NOT NULL
);
ON entries (at);src/book.rs, with sqlx:
use Path;
use ;
Each method returns a soda::Result, so pages and actions can use ?. A soda::Error carries a
message for the person using the app. Soda logs nothing itself: unavailable prints the database's
error for you. If start returns an error, the server stops and prints it.
Name the state in src/main.rs:
app!;app! reads src/routes at compile time. It generates the router, a typed link for every page
(link::about()), the app's Cx, Act and prelude, and main. The template's Cargo.toml
already has sqlx (and tokio, for tests). build.rs rebuilds when a migration is added.
3. Render the page#
src/routes/page.rs is /. It imports Everything, the download from step 7, so it won't
compile until then: leave that import and the "Download them" link out until you get there.
use ;
use Everything;
use crate*;
pub async - A page can be an
async fn. It readscx.state(the book) andcx.session(the browser's session, from a cookie). cx.recall("name")is a value this session saved earlier: here, the last name it signed with. Sessions survive a server restart.- In
view!, text goes in quotes and Rust in braces. Any expression can be an attribute value. Card,Field,Input,Textarea,Button,EmptyandTimeare Soda components (Reference).<Time at=…/>shows "5 minutes ago" in the reader's time zone.headsets the tab title.
4. Post a form#
The form posts to Sign. Write it in src/routes/actions.rs:
use crateEntry;
use crate*;
pub async #[derive(Form)]parses the fields. A missing or too-long field shows an error under it, and the action doesn't run.#[action]generatesSign, which the form names, at/api/_/sign.cx.remembersaves the name for this session.cx.keepkeeps the name field filled when the form clears.cx.sayshows a toast.
When an action finishes, every open page re-renders. The other window shows the new note with no extra code.
5. Check who's acting#
The page only shows "Take back" on your own notes, but anyone can post to an action. So the action checks too:
pub async A button passes arguments: on:click=TakeBack(e.id.clone()) posts the id as entry. Returning
Err with a message refuses the request: the person sees the message and nothing changes. Soda
rejects cross-site requests before any action runs.
6. Add a page and a layout#
src/routes/about/page.rs is /about:
use crate*;
src/routes/layout.rs wraps every page:
use crate*;
Links are typed: renaming or removing a page is a compile error at every link to it. cx.link
marks the current page, so the tab shows as selected. Following a link updates the page in place,
and Back works. src/routes/not_found.rs renders unknown addresses.
7. Offer a download#
src/routes/downloads.rs:
use crate*;
pub async Link to it like a page: href=Everything download. For a page to print instead of a file, use
Download::page.
8. Change the look#
web/app.css holds the design tokens. Every color, font, radius and shadow in the views must be
one (bg-surface, text-muted, rounded-card); the build fails on any that isn't. Warm the
accent:
@}Save, and open pages pick up the new styles in about 0.2 s. See Styling.
What soda dev does#
| You change | Result |
|---|---|
| A page, layout, component or action | Patched in about a second. Pages re-render; data, sessions and open pages stay |
| A new route | Patched the same way |
web/app.html | Pages reload |
web/app.css | Styles swap in place |
A type's fields (say, on Book) | Full rebuild and restart. Pages reload; the database and sessions stay |
| A compile error or a panic | Shown over the page, with its location, until you fix it |
println! output goes to the soda dev terminal. More in soda dev.
9. Test it#
tests/e2e.mjs runs the built server on a free port, with its own SODA_DATA, and drives two
people in Chromium and WebKit. Ada signs and Bob sees it live; Bob can't take back Ada's note; the
download has the book; and a server restart keeps the book and the sessions. Copy its patterns: wait
for what the page shows, never for time.
10. Ship it#
A host needs the program, dist/ and a data folder:
| Variable | Sets |
|---|---|
SODA_ADDR | The address to listen on, e.g. 0.0.0.0:8080 |
SODA_DATA | The data folder |
SODA_DIST | Where dist/ is |
SODA_DATA and SODA_DIST default to data/ and dist/ in the folder the program was built in,
so a host that has only the program and dist/ must set both.
Or let soda deploy build a container image (about 150 MB, serving on 8080, data in a volume):
--to takes any host Docker can reach (ssh://…, tcp://…). Each deploy replaces the running
container and keeps the guestbook-data volume, so the book and sessions survive. A later deploy
rebuilds only what changed (about 11 s for a one-line page change).
As it grows#
- Schema changes. Add a migration (
0002_entries_by_name.sql); it runs once at startup. - Fewer re-renders. A page declares what it shows with
cx.listen("book"); an action declares what it changed withcx.notify("book"). Then pages listening for something else are skipped. Pages that listen for "book", pages that never calllisten, and the acting session's pages still re-render. If a page shows something an action changed without listening for it,soda devsays so over the page. - Your own routes. For a webhook, feed or health check,
App::routesreturns an axumRouterwith the app's state. A handler that changes what pages show takes thePagesextractor and callspages.notify(["book"]). See Reference.
Next#
- Reference: components,
view!, routes, actions, forms,Cx, the CLI. - Events: an app built on events, with history and undo.
- Local-first: the same framework, running in each person's browser.
- Styling: tokens and components.
This page is docs/tutorial.md in the SDK, built into this site as it is.