Commit one move with request
Alice chooses red7 and submits landing square 15 with expected revision zero. A request exchange carries this single input to the rules worker. The worker runs the stored checkers/move mutation and returns one result. The event exchange itself does not commit a move.
The mutation checks the whole starting position and changes the piece, turn and revision together. The first attempt returns committed: true at revision one. Running the same parameters again returns committed: false, because that starting position no longer exists. The example checks both results.
Only after a successful commit does the worker publish a turn notification and an animation cue. Both carry revision one so Bob can associate them with the stored turn. The animation path is [11, 15], whose order describes movement. Bob can avoid repeating an animation for the same revision or read the board when a revision is missing.
A database commit and event publication are separate operations. If the process stops between them, the board contains the move without its notification. A missing response also does not prove that the move failed. Read the board before deciding whether to retry.
The stored request/checkers/move definition supplies schemas for request and response messages. Schema validation checks message shape, while the mutation checks whether the turn can commit. The worker unregisters its route when this example exchange ends.
The successful response is:
{
"committed": true,
"revision": 1
}
Define the messages
The game data installs request/checkers/move in the events definition folder:
{
"request": {
"type": "object",
"required": [
"piece",
"landing",
"expected"
],
"additionalProperties": false,
"properties": {
"piece": {
"type": "string"
},
"landing": {
"type": "integer",
"minimum": 0,
"maximum": 31
},
"expected": {
"type": "integer",
"minimum": 0
}
}
},
"response": {
"type": "object",
"required": [
"committed",
"revision"
],
"additionalProperties": false,
"properties": {
"committed": {
"type": "boolean"
},
"revision": {
"type": "integer",
"minimum": 0
}
}
}
}
The SDK plugin derives the payload types and route helpers from this definition. The Events plugin validates each message against its stored schema. The example below imports the generated SDK.
Code
func moveOnePiece(
game *Game,
alice, bob, rules *events.Owner,
subscription *events.Subscription[events.CheckersTableCursorData],
) {
worker, err := events.ServeCheckersMove(rules)
check(game.t, err)
defer worker.Close()
turns, err := events.SubscribeCheckersTableTurn(bob)
check(game.t, err)
defer turns.Close()
animations, err := events.SubscribeCheckersTableAnimate(bob)
check(game.t, err)
defer animations.Close()
caller, err := events.OpenCheckersMove(alice, events.CheckersMoveRequest{
Piece: "red7",
Landing: 15,
Expected: 0,
})
check(game.t, err)
exchange, err := worker.Accept(time.Second)
check(game.t, err)
request := exchange.Request()
parameters := map[string]any{
"?piece": request.Piece,
"?landing": request.Landing,
"?expected": request.Expected,
}
committed := game.commit("checkers/move", parameters)
reply := events.CheckersMoveResponse{
Committed: committed["committed"].(bool),
Revision: game.revision(),
}
if !reply.Committed || reply.Revision != 1 {
game.t.Fatal("move did not commit")
}
delivery, err := events.PublishCheckersTableTurn(game.db, events.CheckersTableTurnData{
Revision: 1,
Turn: "black",
})
check(game.t, err)
if delivery.Delivered != 1 {
game.t.Fatal("turn not delivered")
}
delivery, err = events.PublishCheckersTableAnimate(game.db, events.CheckersTableAnimateData{
Revision: 1,
Piece: "red7",
Path: []int64{11, 15},
})
check(game.t, err)
if delivery.Delivered != 1 {
game.t.Fatal("animation not delivered")
}
check(game.t, exchange.Respond(reply))
response, err := caller.Response(time.Second)
check(game.t, err)
if response != reply {
game.t.Fatal("incorrect move reply")
}
turn, err := turns.Receive(time.Second)
check(game.t, err)
animation, err := animations.Receive(time.Second)
check(game.t, err)
if turn.Data.Revision != 1 || !reflect.DeepEqual(animation.Data.Path, []int64{11, 15}) {
game.t.Fatal("incorrect turn cue")
}
if game.commit("checkers/move", parameters)["committed"] != false {
game.t.Fatal("stale request committed")
}
}