The platform
Two platforms, one app: native on the server, wasm in the browser
In Roc, a program runs on a platform: a host, written in another language, that gives it its effects (HTTP, a database, the DOM). A Roc Solid app is two programs. On the server, your Roc compiles to native code, linked with a Rust host into one executable. In the browser, the same pages compile to WebAssembly with rocx-engine, the Rust engine that keeps their signals, and a small script applies its changes to the page. The same engine renders the server's pages.
Two platforms, one app#
server/: native
Your Roc as machine code, linked with a Rust host (tokio, hyper) into one executable. It renders pages, answers endpoints and keeps the app's state.
HTTP
web/: wasm
The app's components as WebAssembly with rocx-engine (Rust), which keeps the signals and runs what changes, plus web/engine.js: it takes over the server's page, applies the engine's changes, and sends requests.
The server renders each page to HTML. The browser then hydrates it: it runs the same components, attaches them to the existing HTML, and then updates only what changes (components explains signals and holes). The two talk over plain HTTP: the browser reads and sends endpoints and listens to channels.
Both platforms compile the same component code. Its modules live in the browser platform (web/platform), and the build copies them into the server's: Html, Solid, Store, Repeat, Loading, Errored, Context, Transition, Router, Search, Leave, Title, Submission, Submissions, Browser and HttpStatusCode for components; Server, Endpoint, FormBody, Call, Channel, Events, Middleware and OpenApi for HTTP. The server renders a page with the engine the browser runs (its server mode), so hydration finds what it expects.
What the server gives you#
Requests that wait cheaply
Each request is a call that pauses while it waits on the database or another server. A crash ends that call only.
HTTP, both ways
Http.fetch! with time and size limits; raw-byte bodies; answers passed through from elsewhere; cookies with every attribute.
Live updates
Typed, access-checked messages to pages, over one stream per tab. Rooms count who is here; topics carry messages between calls.
The database
Postgres, or SQLite in the server's process (SQLite's engine or Turso's). Typed rows, transactions, migrations built into the executable.
Actors
State that handles one message at a time, with typed asks, tells and timers. Kept in the database, so it survives a restart.
Values in memory
Kv: typed values by key, with a time to live and atomic operations. For rate limits, caches, one-time tokens.
Settings
Environment variables (and the .env file rocx dev passes on), the app's public URL, and development mode.
Crypto
Secure random bytes, AES-GCM and P-256 (ES256, ECDH) from the host; SHA-256, HMAC, HKDF and UUIDs from the crypto package.
Plugins
A package adds tables, background loops and files to an app in one line. Jobs and cron (packages/jobs) and email are plugins.
The Rust host gives Roc only general building blocks: HTTP, database connections, TCP and TLS sockets, background loops, isolated calls, actors' mailboxes and timers, values in memory. Migrations, plugins and actors' state are Roc on top of those (what is Rust, what is Roc). What needs neither the host nor the compiler is a package in the ecosystem: file storage (S3, a directory, memory), the crypto written in Roc, jobs and cron, email.
What an app gives the server#
You write pages, layouts, endpoints and middleware in routes/, and rocx writes the server's program from them (Generated/App). Your main.roc adds what routes can't say: how to make the app's context at start (init!), and, if needed, the database, plugins, whether other sites may post to it (cross_site) and the places --dev lists (links). This site's:
program = App.program({
init!: |started| Ok({ facts: started }),
…
plugins: [AudioPlayer.plugin({})],
})An app that names no database has SQLite's data/app.db. SQL files in migrations/ are built into the executable (migrations), and actors are found in the app's modules. App.program returns the record the server platform takes:
init!: makes the app's context once, at start, from the server's--context-file. Every request, actor turn and plugin call gets it.respond!: answers aServer.Request(method, path, query, headers, body as bytes, the matched route, andpeer, the client's address) with aServer.Response. The routes rocx writes do this (routing).database:Postgres,Sqlite(path)orTurso(path).migrations: the app's schema, each a name and its SQL.actors: the methods of the app's actor kinds, so the server can run one when nobody waits on it (a tell, a timer).logs: the log's format (Auto,PrettyorJson).pluginsandroutes: what packages add, and the app's own paths; the server checks them for clashes at start.
You can also write a program by hand, as the platform's test app does:
program = {
…
init!: |started| {
…
respond!,
…
# The app's schema, in the executable: applied once each, in order, when the server starts.
migrations: [{ name: "0001_tables.sql", sql: tables }, { name: "0002_note_index.sql", sql: note_index }, { name: "0003_admin.sql", sql: admin_tables }, { name: "0004_nested.sql", sql: nested_tables }],
# The actors' methods (`rocx build` lists them for an app it builds).
actors: [Actor.handler(Counter.add), Actor.handler(Counter.get), Actor.handler(Counter.boom), Actor.handler(Counter.nap), Actor.handler(Mirror.relay), Actor.handler(Mirror.itself), Actor.handler(Mirror.cycle), Actor.handler(Bouncer.back), Actor.handler(Ticker.start), Actor.handler(Ticker.tick({})), Actor.handler(Ticker.stop), Actor.handler(Ticker.ticks)],
…
# The plugins (`plugins`): the platform runs what they add (no routes, D-116).
plugins: plugins.map(Plugin.running),
routes: [],
}A response may be text, bytes, a page of components, a stream of server-sent events (a loop run as its own call), or a body fetched from another server. The browser's program is the root of components the server rendered; rocx writes it into the browser's copy of the app (build/client):
## The browser's app, written by rocx build --client from site: the app's page (`Generated/Root`),
## which the browser hydrates (web/start.js), on the web platform. Don't edit it.
app [main!] { admin: "../../../packages/admin/browser.roc", audio: "../../../packages/audio/browser.roc", charts: "../../../packages/charts/main.roc", forms: "../../../packages/forms/main.roc", guard: "../../../packages/guard/browser.roc", intl: "../../../packages/intl/main.roc", markdown: "../../../packages/markdown/main.roc", mcp: "../../../packages/mcp/browser.roc", presence: "../../../packages/presence/browser.roc", push: "../../../packages/push/browser.roc", qr: "../../../packages/qr/main.roc", testing: "../../../packages/testing/main.roc", tools: "../../../packages/tools/main.roc", video: "../../../packages/video/main.roc", virtual: "../../../packages/virtual/main.roc", pf: platform "../../../web/platform/main.roc" }
import pf.Html
import Generated/Root
main! : {} => Html
main! = |{}| Root.view!({})
# rocx: generated from the routes: rocx build --client writes it.A request, from start to end#
- server The host (hyper) reads the request and its body (16 MiB at most, unless
--max-bodyallows more). It starts a guarded call into Roc on one of Roc's threads. When an effect waits, the call pauses and the thread runs other calls. - server The app's
respond!runs its middleware, then the route. A page's answer is one tree: the layouts, the page and their components. An effect (a query, a queued job) pauses this call only. - server The host renders the tree to HTML, running each hole once. An endpoint a component reads is answered in-process, through its route. The answers go into a script at the end of the page, then
/rocx/start.js. Markup inside<NoHydration>is plain HTML, for the server alone. - browser The page shows at once. The script loads the wasm and hydrates: the components run again, attach to the server's HTML, and take endpoint answers from the page, not the server. A
<NoHydration>stays as it came. - browser A click writes a signal. Only the holes that read it run again, each patching its own node. A read the page has no answer for (a URL built from a signal, or a
refresh!) becomes a GET of the endpoint's URL, answered through the same route.
Building and running#
rocx dev builds both programs, serves the app on port 8080 in development mode, and rebuilds and restarts it on each save (tooling). A SQLite app needs nothing else. A Postgres app needs a database URL: in this repository, just db starts one in Docker and prints it.
rocx dev my-app # http://localhost:8080, rebuilt on save
rocx dev my-app -- --database-url postgres://… # a Postgres app: the server's options after --
rocx check my-app # both programs type-checked, problems at .rocx linesA release build is one command (rocx):
rocx build my-app [--target arm64musl]
my-app/dist/server --port 8080 --static my-app/dist/static
rocx build my-app --image # my-app/my-app.tar, for docker load -irocx build turns the .rocx files into Roc and compiles the browser's copy to wasm, optimized for size. It compiles the server's program, optimized for speed, and links it with the Rust host into dist/server, migrations and plugins' files inside, and only the database engine the program names; dist/static holds the files it serves. --image writes a container image of it, without a Dockerfile. --target arm64musl (or x64musl) cross-compiles a static Linux executable for a container. The executable takes its port, database and the rest as flags or environment variables (running it). Each example's README shows the steps one at a time.
Why this shape#
- Native on the server. Roc compiles to machine code: no wasm runtime in between. Rust does what Roc can't: sockets, TLS, threads.
- Wasm in the browser. Only components that hydrate go to the browser: never an endpoint's handler, nor markup inside
<NoHydration>. - One component model, one engine. Solid-style components on both sides, run by the same Rust engine: the server renders what the browser hydrates, with the data it needs. The phones' experimental hosts run it too.
- One server. An app runs as one instance with one database, not as copies behind a load balancer. Rooms, topics, channels,
Kvand actors' mailboxes live in its memory; actors' state and jobs in its database.