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

Roc Solid from the database to the browser

Get startedRead the guide

What Roc Solid is#

Roc Solid is a web framework for Roc, a functional language that compiles to native code and to WebAssembly. You write components in .rocx files: Roc with HTML-like markup. The counter below is one. Step through how it gets from its file to your screen.

Counter.rocx

Roc, with markup wherever an expression can go. count! reads a signal; set_count! changes it.

view! = |{ start_value }| {
	(count!, set_count!) = create_signal!(start_value)
	<div class="counter" class:negative={count!() < 0}>
		<button type="button" on:click={|| set_count!(count!() - 1)} aria-label="One less">−</button>
		<output>{count!().to_str()}</output>
		<button type="button" on:click={|| set_count!(count!() + 1)} aria-label="One more">+</button>
	…
	</div>
}

Counter.roc

rocx build turns the markup into plain Roc, line for line, so the Roc compiler checks every value in it.

view! = |{ start_value: start_value! }| {
	(count!, set_count!) = create_signal!(start_value!())
	Html.element("div", [Html.attr_literal("class", "counter rx-49lxy6"), Html.class_of("negative", || count!() < 0),], [
		Html.element("button", [Html.attr_literal("type", "button"), Html.on("click", || set_count!(count!() - 1)), Html.attr_literal("aria-label", "One less"), Html.attr_literal("class", "rx-49lxy6"),], [Html.text("−"),]), Html.text(" "),
		Html.element("output", [Html.attr_literal("class", "rx-49lxy6"),], [Html.hole_of(|| Html.hole(count!().to_str())),]), Html.text(" "),
		Html.element("button", [Html.attr_literal("type", "button"), Html.on("click", || set_count!(count!() + 1)), Html.attr_literal("aria-label", "One more"), Html.attr_literal("class", "rx-49lxy6"),], [Html.text("+"),]), Html.text(" "),
…
}

What the server sends

The server runs that Roc, compiled to native code, and sends plain HTML: readable before any script runs. <!--x--> marks the text the count changes.

<div class="counter rx-49lxy6">
  <button class="rx-49lxy6" type="button" aria-label="One less">−</button>
  <output class="rx-49lxy6"><!--x-->3<!--/x--></output>
  <button class="rx-49lxy6" type="button" aria-label="One more">+</button>
  <button class="reset rx-49lxy6" type="button">Reset</button>
</div>
…
<script type="module" data-rocx data-build="…" src="/rocx/start.js"></script>

In your browser

The browser runs the same component, compiled to WebAssembly, over that HTML. It attaches handlers and signals to the nodes already there, and builds nothing again.

<div class="counter">      ← class:negative reads count
  <button>−</button>       ← on:click
  <output>3</output>       ← its text reads count
  <button>+</button>       ← on:click
  <button>Reset</button>   ← on:click; disabled reads count
</div>

Waiting for the counter to wake up…

What the runtime did

Press − or +. The handler sets the signal, and only what reads it runs again, patching its own node. Newest first:

  1. Nothing yet: press − or + below.

Live

3

An app gets:

Getting started#

  1. 1

    Install

    One command installs rocx, its Roc compiler and both platforms, prebuilt, into ~/.rocx (macOS on Apple silicon, Linux on arm64 or x86_64). No Rust, Zig or Node needed. It verifies each download and adds ~/.rocx/bin to your PATH. With Nix: nix profile install github:Roc-Solid/rocx.

    curl -fsSL https://github.com/Roc-Solid/rocx/releases/latest/download/install.sh | sh

    To work on Roc Solid itself, build it from a clone with git and just: just setup fetches the rest into .tools/ and builds bin/rocx.

    git clone https://github.com/Roc-Solid/rocx
    cd rocx
    just setup
    export PATH="$PWD/bin:$PATH"
  2. 2

    Make an app

    rocx new makes an app in its own folder and asks which database: SQLite (the default, a file beside the server), Postgres or Turso. It comes with sign-in by a mailed link (or Google and GitHub, once their keys are set), optional two-factor codes, a users table, refusal of other sites' posts, security headers, and rate-limited sign-in mail.

    rocx new my-app
    cd my-app
    rocx dev

    rocx dev serves it at http://localhost:8080. Each save rebuilds it, restarts the server and reloads your open pages; a mistake shows at its line, and the last good build keeps running. Under rocx dev, sign in with any address, no mail needed. The app's README.md says what to change first, and how to test (rocx test), deploy (rocx build or rocx build --image) and upgrade (rocx upgrade).

  3. 3

    Add a page, or an endpoint

    Each file in routes/ answers the URL its folders spell; that folder is all an app needs. A +page.rocx is a page: rendered to HTML on the server, made live in the browser. A +server.roc is an endpoint: functions named after HTTP methods (get!, post!) that answer with JSON.

    my-app/routes/+page.rocx
    import pf.Html
    import pf.Solid exposing [create_signal!]
    
    ## The page at /: rendered by the server, then live in the browser.
    view! = |_| {
    	(count!, set_count!) = create_signal!(0.I64)
    	<main>
    		<h1>Hello, Roc Solid</h1>
    		<button on:click={|| set_count!(count!() + 1)}>
    			Clicked {count!().to_str()} times
    		</button>
    	</main>
    }
    my-app/routes/hello/+server.roc
    import pf.Server
    import pf.Endpoint
    
    ## GET /hello: a greeting, as JSON.
    get! : { request : Server.Request, params : {}, context : c } => Try(Str, Endpoint.Refusal)
    get! = |_| Ok("Hello from the server")

    view! runs once: it makes a signal and returns markup. The button's text reads the count, so it updates when the count does. get! answers GET /hello with a JSON string.

Next: rocx covers pages, components and routes; syntax lists everything markup can hold; tooling covers every command; the platform is what the server and the browser give an app; and the examples are complete apps to read.

Three parts#

rocx

What you write: Roc with markup wherever an expression can go. rocx build turns each page, layout and component into plain Roc with the same line numbers, for the compiler to check.

rocx →
view! = |{ start_value }| {
	(count!, set_count!) = create_signal!(start_value)
	<div class="counter" class:negative={count!() < 0}>
		<button type="button" on:click={|| set_count!(count!() - 1)} aria-label="One less">−</button>
		<output>{count!().to_str()}</output>
		<button type="button" on:click={|| set_count!(count!() + 1)} aria-label="One more">+</button>
	…
	</div>
}

Platform

What an app runs on. On the server: native Roc in a Rust host, with SQLite, Postgres or Turso, a key-value store, live channels, actors, and plugins that let packages add tables, jobs and scripts. In the browser: the same components in WebAssembly with the Rust engine that runs them, and a small script that puts its changes on the page.

The platform →
## The list `id` (as its URL has it): its name.
list! : Str => Try({ id : Str, name : Str }, Problem)
list! = |id| {
	key = number(id)?
	row : { name : Str }
	row = Db.one!("select name from lists where id = $1", [Int(key)])?
	Ok({ id, name: row.name })
}

Ecosystem

Packages of plain Roc and components, for both halves of an app. Twenty-six so far: sign-in, Stripe payments, Claude, charts, crypto, file storage (S3, disk or memory), email, forms, Markdown, intl, an admin, audio, guards, jobs and cron, MCP, tools for models, OpenTelemetry, PostHog, presence, Web Push, QR codes, vibration, testing, headless Chromium, videos and virtual lists.

The ecosystem →
import charts.BarChart
import charts.Format

{<BarChart data!={walk!().birds} label="Birds counted on a morning walk, by kind" format={|count| Format.num(count, 0)} padding={Shuffle.pick(look!(), 0.3, 0.55, 0.12)} />}

Measured on this build#

Wasm for the whole site
475.2 KB
brotli; 644.3 KB gzip, 1907.5 KB raw
JavaScript
24.6 KB
gzip; no framework, no dependencies
The front page, rendered
651 µs
by the browser's wasm, in Node
The server
41215.7 KB
one executable, every page in it