Your first surface¶
This tutorial puts a surface into an ankka service and drives it over HTTP with curl, the way a
device would. It takes about fifteen minutes once you have a Scala service. You need the JDK 21, sbt,
Docker, and a service made from ankka's template, as
Your first service in Scala makes one.
Take the library¶
ankka-surface has no release yet, so a service on this machine takes it from ~/.ivy2/local. In a
checkout of ankka-surface:
sbt 'set ThisBuild / version := "0.1.0-SNAPSHOT"' publishLocal
The library is built against ankka 0.16.3. A service on another ankka version builds the library
against its own with sbt -Dankka.version=<version> ..., because a Scala library and the platform it
runs on must agree on one ankka. Then, in the service's build.sbt:
libraryDependencies += "com.thinkmorestupidless" %% "ankka-surface" % "0.1.0-SNAPSHOT"
Declare the vocabulary and the interpreter¶
The three samples in this section and the next are excerpts of one file,
Fixtures.scala,
which imports com.thinkmorestupidless.ankka.surface.* and
com.thinkmorestupidless.ankka.core.ComponentId. Put them at the top level of a file in the service's own package.
A surface holds only words its consumer declares. This vocabulary names three shapes with home as
the one a new surface opens on, two anchor kinds, one modifier key, one pending kind and two focus
keys:
val vocabulary: Vocabulary = Vocabulary(
shapes = Set("home", "shelf", "item"),
homeShape = "home",
anchorKinds = Set("theme", "market"),
modifierKeys = Set("time"),
pendingKinds = Set("forecast"),
focusKeys = Set("card", "outcome")
)
The interpreter turns what a person said into a move. This one understands any words naming Newcastle and nothing else:
object NewcastleInterpreter extends Interpreter:
def interpret(surface: Surface, text: String): Interpretation =
if text.toLowerCase.contains("newcastle") then
Interpretation.Understood(
Move(shape = Some("shelf"), anchor = Some(Anchor("theme", "newcastle")))
)
else Interpretation.NotUnderstood(s"nothing on the shelves matches '$text'")
Register the components¶
A surface is four components, each one an object that extends the library's companion with a
component id of the service's choosing. The last two are the focus lane, which is optional:
object Surfaces extends SurfaceCompanion(ComponentId("surface"), vocabulary)
object Rows extends SurfaceRowsCompanion(ComponentId("surface-rows"), Surfaces)
object Focuses extends SurfaceFocusCompanion(ComponentId("surface-focus"), vocabulary)
object FocusRows extends SurfaceFocusRowsCompanion(ComponentId("surface-focus-rows"), Focuses)
Register all four in the service's Main.scala, with the projection runtime that keeps the rows and
the endpoint mounted under /surfaces. This is the template's Main with the surface in it; it is
written by hand, and Serve surfaces says what each argument does:
import com.thinkmorestupidless.ankka.http.{Acl, HttpServer}
import com.thinkmorestupidless.ankka.runtime.{Ankka, ProjectionRuntime}
import com.thinkmorestupidless.ankka.surface.{FocusLane, SurfaceEndpoint}
@main def run(): Unit =
val service = Ankka.service
.register(Surfaces.descriptor)
.register(Rows.descriptor)
.register(Focuses.descriptor)
.register(FocusRows.descriptor)
.withExtension(ProjectionRuntime())
.withExtension(HttpServer.of(clients =>
SurfaceEndpoint("/surfaces", Acl.AllowAll, clients.componentClient, clients.viewClient,
Surfaces, Rows, NewcastleInterpreter, focus = Some(FocusLane(Focuses, FocusRows)))
))
.start()
sys.addShutdownHook(service.terminate())
scala.concurrent.Await
.result(service.whenTerminated, scala.concurrent.duration.Duration.Inf): Unit
Acl.AllowAll lets anyone call every route, which is right for this tutorial and wrong for anything a
person relies on.
Start it¶
sbt schema # writes ankka's schema into target/ddl
docker compose up -d # Postgres on 5432, initialised from target/ddl
sbt run # the service, HTTP on :9000
Open a surface¶
A surface nobody has opened holds nothing and refuses every move. Opening it gives it an owner and the vocabulary's home shape:
curl -XPOST localhost:9000/surfaces/ada/open -H 'content-type: application/json' -d '{"owner":"ada"}'
{"id":"ada","owner":"ada","opened":true,"shape":"home","anchor":null,"modifiers":{},"pending":null,"rail":[],"showing":[],"version":1}
Move it¶
One move changes as many parts as it names, all together, and raises the version by one. A move with
a request puts it on the rail with the surface it left:
curl -XPOST localhost:9000/surfaces/ada/moves -H 'content-type: application/json' \
-d '{"shape":"shelf","anchor":{"kind":"theme","id":"newcastle"},"modifiers":{"time":"this weekend"},"request":"anything about Newcastle this weekend"}'
{"id":"ada","owner":"ada","opened":true,"shape":"shelf","anchor":{"kind":"theme","id":"newcastle"},"modifiers":{"time":"this weekend"},"pending":null,"rail":[{"text":"anything about Newcastle this weekend","shape":"shelf","anchor":{"kind":"theme","id":"newcastle"},"modifiers":{"time":"this weekend"},"at":"2026-11-13T09:00:04Z"}],"showing":[],"version":2}
A word the vocabulary lacks is refused before anything is kept, and the refusal names it:
curl -XPOST localhost:9000/surfaces/ada/moves -H 'content-type: application/json' -d '{"shape":"basket"}'
# 400 {"status":400,"error":"the vocabulary has no shape 'basket'"}
Say something¶
say hands the words to the interpreter with the surface as it stands. Words it does not understand
change nothing, and the answer says why:
curl -XPOST localhost:9000/surfaces/ada/say -H 'content-type: application/json' -d '{"text":"make me a sandwich"}'
{"understood":false,"reason":"nothing on the shelves matches 'make me a sandwich'","surface":{"id":"ada","shape":"shelf","version":2,"...":"the surface, unchanged"}}
That reply is shortened: surface is the whole surface, as GET /surfaces/ada returns it. Words it
does understand are applied as a move with the words as its request.
Hold an act without taking it¶
A move may hold a pending act: something the person has begun and not committed, shown on every device. The surface keeps it and does nothing else with it:
curl -XPOST localhost:9000/surfaces/ada/moves -H 'content-type: application/json' \
-d '{"pending":{"kind":"forecast","fields":{"outcome":"Yes","estimate":"70"}}}'
# 200, the surface with "pending":{"kind":"forecast","fields":{"outcome":"Yes","estimate":"70"}}
Committing the forecast is a route of the service's own; The surface never acts says why.
Show it on a device and stream it¶
A device records itself as showing the surface, with its class and whether it is in a room:
curl -XPOST localhost:9000/surfaces/ada/show -H 'content-type: application/json' \
-d '{"device":"kitchen-tv","deviceClass":"tv","room":true}'
# 200, the surface with "showing":[{"device":"kitchen-tv","deviceClass":"tv","room":true,"since":"..."}]
In a second terminal, stream it. The stream gives the surface now, says it has caught up, and then gives every version written while it is open:
curl -N localhost:9000/surfaces/ada/stream
event:row
data:{"key":"ada","row":{"id":"ada","owner":"ada","opened":true,"shape":"shelf",...,"version":6}}
event:caught-up
data:{}
Hide the television from the first terminal and the stream gives version 7, with showing empty.
Stop the service and start it again: the surface reads as it did, rebuilt from its journal.
Close it¶
curl -XDELETE localhost:9000/surfaces/ada
# 204
The stream says removed, GET /surfaces/ada answers 404, and the id is free to open again.
Next¶
- Put a device in front of it: Render and steer on a device.
- Write an interpreter that does real work: Write an interpreter.
- Every route, body and refusal: HTTP API.