Roc Solid
Markup in Roc · pages on a native server · components in WebAssembly

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.

The serverThe browser

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#

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:

site/main.roc, in part
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:

You can also write a program by hand, as the platform's test app does:

server/test/app/main.roc, in part
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):

site/build/client/main.roc
## 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#

  1. server The host (hyper) reads the request and its body (16 MiB at most, unless --max-body allows 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.

Developing
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 lines

A release build is one command (rocx):

A release build
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 -i

rocx 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#