Skip to content

Serve surfaces

A consumer serves surfaces by registering the library's components in its own ankka service and mounting SurfaceEndpoint on its HTTP server. The components are two for the surface and two more for the optional focus lane.

Declare the components

Each component is an object that extends the library's companion with a component id of the consumer's choosing:

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)

The component id is the consumer's so that two kinds of surface in one service, with two vocabularies, keep apart: each gets its own entity, rows and endpoint prefix. A component id is stored with every event, so it does not change once a surface has been opened under it.

Mount the endpoint

The endpoint takes the prefix, the ACL, the two clients the HTTP server hands it, the two surface components, the interpreter, and optionally a clock and the focus lane. In the library's own HTTP suite it is mounted like this:

HttpServer.at("127.0.0.1", 0)(clients =>
  SurfaceEndpoint(
    "/surfaces",
    Acl.AllowAll,
    clients.componentClient,
    clients.viewClient,
    Surfaces,
    Rows,
    interpreter,
    ticking,
    focus = Some(FocusLane(Focuses, FocusRows))
  )
)

A service binds its HTTP server with HttpServer.of, as ankka's template does; the suite uses HttpServer.at to bind a free local port.

Argument What it is
prefix the path every route is under; a device's baseUrl is this prefix as the device reaches it
acl who may call the routes; one ACL for every route
client, views clients.componentClient and clients.viewClient
surfaces, rows the two surface companions
interpreter the consumer's interpreter
clock where every command's time comes from; Clock.systemUTC() unless replaced
focus Some(FocusLane(focuses, focusRows)) for the focus routes; none by default

Register them

Every component the endpoint uses is registered, with the projection runtime that keeps the two views:

testKit = AnkkaTestKit.start(
  Seq(Surfaces.descriptor, Rows.descriptor, Focuses.descriptor, FocusRows.descriptor),
  Seq(server, ProjectionRuntime())
)

A service does the same with Ankka.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()

Without ProjectionRuntime every move succeeds and no device is ever streamed anything, because the rows are what a device streams.

Choose the ACL

The library holds the owner's name and nothing else about them. Who may read and steer a surface is the consumer's rule, given as the endpoint's ACL, such as Acl.Authenticate(…) to require a signed-in caller or Acl.AllowIf(…) for a rule of the consumer's own. Acl.AllowAll is for a tutorial or a test. ankka's HTTP endpoints guide says what each ACL admits.

The ACL decides whether a caller may reach the routes, not whose surface they reach: the library does not compare the caller with a surface's owner. A consumer that needs each person to reach only their own surfaces names them by ids only their owner is given, or puts its own route in front of the endpoint that makes the comparison.

Replace the clock

The surface reads no clock: every command carries its time, stamped by the endpoint. A test passes a clock of its own so every time in a run is known; the library's HTTP suite uses one that ticks a second per read.

List an owner's surfaces

GET {prefix}/?owner=<owner> answers every opened surface whose owner is that name, read from the rows view. A person usually has one surface; a consumer that opens several per person, one per room or one per task, uses this to offer them.

The listing is read from the view, so a surface opened a moment ago may not be in it yet, and the surfaces in it may be a version or two behind. GET {prefix}/{id} reads the entity and is always current.