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:
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 00 | 54 52 4F 4E | magic TRON |
| 04 | 8C 7265766973696F 6E | txt "revision" |
| 0D | 02 01000000000000 00 | i64 1 |
| 16 | 0F 0A 04000000 0D00 0000 | map leaf, size 10: @4 → @D |
| 20 | 9C 636F6D6D697474 6564 | txt "committed" |
| 2A | 09 | bit true |
| 2B | 0F 0A 20000000 2A00 0000 | map leaf, size 10: @20 → @2A |
| 35 | 07 0E 00030000 1600 0000 2B000000 | map branch, size 14, depth 0 (root), slots 8, 9: @16 → @2B |
| 43 | 35000000 00000000 | root → @35; previous: none |
Define the messages
The game data installs request/checkers/move in the events definition folder:
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 000 | 54 52 4F 4E | magic TRON |
| 004 | 5C 7069656365 | txt "piece" |
| 00A | 7C 6C616E64696E67 | txt "landing" |
| 012 | 8C 65787065637465 64 | txt "expected" |
| 01B | 0E 15 00 0700 030000 00 04000000 0A0000 00 12000000 | array leaf, size 21, shift 0, bitmap 0007, count 3 (root): @4 → @A → @12 |
| 030 | 4C 74797065 | txt "type" |
| 035 | 6C 737472696E67 | txt "string" |
| 03C | 0F 0A 30000000 3500 0000 | map leaf, size 10: @30 → @35 |
| 046 | 7C 6D6178696D756D | txt "maximum" |
| 04E | 02 1F000000000000 00 | i64 31 |
| 057 | 0F 0A 46000000 4E00 0000 | map leaf, size 10: @46 → @4E |
| 061 | 4C 74797065 | txt "type" |
| 066 | 7C 696E7465676572 | txt "integer" |
| 06E | 0F 0A 61000000 6600 0000 | map leaf, size 10: @61 → @66 |
| 078 | 7C 6D696E696D756D | txt "minimum" |
| 080 | 02 00000000000000 00 | i64 0 |
| 089 | 0F 0A 78000000 8000 0000 | map leaf, size 10: @78 → @80 |
| 093 | 07 12 08840000 5700 0000 6E000000 8900 0000 | map branch, size 18, depth 0 (root), slots 3, 10, 15: @57 → @6E → @89 |
| 0A5 | 4C 74797065 | txt "type" |
| 0AA | 7C 696E7465676572 | txt "integer" |
| 0B2 | 0F 0A A5000000 AA00 0000 | map leaf, size 10: @A5 → @AA |
| 0BC | 7C 6D696E696D756D | txt "minimum" |
| 0C4 | 02 00000000000000 00 | i64 0 |
| 0CD | 0F 0A BC000000 C400 0000 | map leaf, size 10: @BC → @C4 |
| 0D7 | 07 0E 00840000 B200 0000 CD000000 | map branch, size 14, depth 0 (root), slots 10, 15: @B2 → @CD |
| 0E5 | 8C 65787065637465 64 | txt "expected" |
| 0EE | 0F 0A E5000000 D700 0000 | map leaf, size 10: @E5 → @D7 |
| 0F8 | 5C 7069656365 | txt "piece" |
| 0FE | 0F 0A F8000000 3C00 0000 | map leaf, size 10: @F8 → @3C |
| 108 | 7C 6C616E64696E67 | txt "landing" |
| 110 | 0F 0A 08010000 9300 0000 | map leaf, size 10: @108 → @93 |
| 11A | 07 12 00610000 EE00 0000 FE000000 1001 0000 | map branch, size 18, depth 0 (root), slots 8, 13, 14: @EE → @FE → @110 |
Show all 76 nodes
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 12C | AC 70726F70657274 696573 | txt "properties" |
| 137 | 0F 0A 2C010000 1A01 0000 | map leaf, size 10: @12C → @11A |
| 141 | 8C 72657175697265 64 | txt "required" |
| 14A | 0F 0A 41010000 1B00 0000 | map leaf, size 10: @141 → @1B |
| 154 | 14 14 616464697469 6F6E616C50726F70 657274696573 | txt "additionalProperties" |
| 16A | 01 | bit false |
| 16B | 0F 0A 54010000 6A01 0000 | map leaf, size 10: @154 → @16A |
| 175 | 4C 74797065 | txt "type" |
| 17A | 6C 6F626A656374 | txt "object" |
| 181 | 0F 0A 75010000 7A01 0000 | map leaf, size 10: @175 → @17A |
| 18B | 07 16 A8040000 3701 0000 4A010000 6B01 0000 81010000 | map branch, size 22, depth 0 (root), slots 3, 5, 7, 10: @137 → @14A → @16B → @181 |
| 1A1 | 9C 636F6D6D697474 6564 | txt "committed" |
| 1AB | 8C 7265766973696F 6E | txt "revision" |
| 1B4 | 0E 11 00 0300 020000 00 A1010000 AB0100 00 | array leaf, size 17, shift 0, bitmap 0003, count 2 (root): @1A1 → @1AB |
| 1C5 | 4C 74797065 | txt "type" |
| 1CA | 7C 626F6F6C65616E | txt "boolean" |
| 1D2 | 0F 0A C5010000 CA01 0000 | map leaf, size 10: @1C5 → @1CA |
| 1DC | 4C 74797065 | txt "type" |
| 1E1 | 7C 696E7465676572 | txt "integer" |
| 1E9 | 0F 0A DC010000 E101 0000 | map leaf, size 10: @1DC → @1E1 |
| 1F3 | 7C 6D696E696D756D | txt "minimum" |
| 1FB | 02 00000000000000 00 | i64 0 |
| 204 | 0F 0A F3010000 FB01 0000 | map leaf, size 10: @1F3 → @1FB |
| 20E | 07 0E 00840000 E901 0000 04020000 | map branch, size 14, depth 0 (root), slots 10, 15: @1E9 → @204 |
| 21C | 8C 7265766973696F 6E | txt "revision" |
| 225 | 0F 0A 1C020000 0E02 0000 | map leaf, size 10: @21C → @20E |
| 22F | 9C 636F6D6D697474 6564 | txt "committed" |
| 239 | 0F 0A 2F020000 D201 0000 | map leaf, size 10: @22F → @1D2 |
| 243 | 07 0E 00030000 2502 0000 39020000 | map branch, size 14, depth 0 (root), slots 8, 9: @225 → @239 |
| 251 | AC 70726F70657274 696573 | txt "properties" |
| 25C | 0F 0A 51020000 4302 0000 | map leaf, size 10: @251 → @243 |
| 266 | 8C 72657175697265 64 | txt "required" |
| 26F | 0F 0A 66020000 B401 0000 | map leaf, size 10: @266 → @1B4 |
| 279 | 14 14 616464697469 6F6E616C50726F70 657274696573 | txt "additionalProperties" |
| 28F | 01 | bit false |
| 290 | 0F 0A 79020000 8F02 0000 | map leaf, size 10: @279 → @28F |
| 29A | 4C 74797065 | txt "type" |
| 29F | 6C 6F626A656374 | txt "object" |
| 2A6 | 0F 0A 9A020000 9F02 0000 | map leaf, size 10: @29A → @29F |
| 2B0 | 07 16 A8040000 5C02 0000 6F020000 9002 0000 A6020000 | map branch, size 22, depth 0 (root), slots 3, 5, 7, 10: @25C → @26F → @290 → @2A6 |
| 2C6 | 8C 726573706F6E73 65 | txt "response" |
| 2CF | 0F 0A C6020000 B002 0000 | map leaf, size 10: @2C6 → @2B0 |
| 2D9 | 7C 72657175657374 | txt "request" |
| 2E1 | 0F 0A D9020000 8B01 0000 | map leaf, size 10: @2D9 → @18B |
| 2EB | 07 0E 40040000 CF02 0000 E1020000 | map branch, size 14, depth 0 (root), slots 6, 10: @2CF → @2E1 |
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 2F9 | EB020000 00000000 | root → @2EB; previous: none |
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")
}
}