Share hand cursors with pub/sub
Alice moves her hand over the board. Bob needs the latest cursor position, but this movement does not change a piece. Publish the position on checkers/table/cursor. The generated subscription helper selects that exact subject and decodes its schema-derived payload type. Turn and animation cues have separate stored publish definitions and typed subscriptions.
The publisher receives delivery counts. Bob receives the subject, data and number of deliveries dropped since his previous receive. The example checks that Alice's cursor arrives and the board revision remains zero. Subscribe before publishing because publications have no replay.
Each subscription has a bounded queue. A slow consumer can lose cursor updates, and the reported drop count lets it detect that loss. A subsequent cursor position can replace an old one. For a missed turn or animation cue, read the board instead of guessing which move happened.
Keep the returned subscription for later steps. The generated owner and subscription manage the plugin IDs.
Alice's cursor data is:
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 00 | 54 52 4F 4E | magic TRON |
| 04 | 6C 706C61796572 | txt "player" |
| 0B | 5C 616C696365 | txt "alice" |
| 11 | 0F 0A 04000000 0B00 0000 | map leaf, size 10: @4 → @B |
| 1B | 1C 79 | txt "y" |
| 1D | 03 CDCCCCCCCCCC08 40 | f64 3.1 |
| 26 | 0F 0A 1B000000 1D00 0000 | map leaf, size 10: @1B → @1D |
| 30 | 1C 78 | txt "x" |
| 32 | 03 CDCCCCCCCCCC18 40 | f64 6.2 |
| 3B | 0F 0A 30000000 3200 0000 | map leaf, size 10: @30 → @32 |
| 45 | 07 12 A0040000 1100 0000 26000000 3B00 0000 | map branch, size 18, depth 0 (root), slots 5, 7, 10: @11 → @26 → @3B |
| 57 | 45000000 00000000 | root → @45; previous: none |
Define the messages
The game data installs publish/checkers/table/cursor in the events definition folder:
| Offset (hex) | Bytes (hex) | Meaning |
|---|---|---|
| 000 | 54 52 4F 4E | magic TRON |
| 004 | 6C 706C61796572 | txt "player" |
| 00B | 1C 78 | txt "x" |
| 00D | 1C 79 | txt "y" |
| 00F | 0E 15 00 0700 030000 00 04000000 0B0000 00 0D000000 | array leaf, size 21, shift 0, bitmap 0007, count 3 (root): @4 → @B → @D |
| 024 | 4C 74797065 | txt "type" |
| 029 | 6C 737472696E67 | txt "string" |
| 030 | 0F 0A 24000000 2900 0000 | map leaf, size 10: @24 → @29 |
| 03A | 4C 74797065 | txt "type" |
| 03F | 6C 6E756D626572 | txt "number" |
| 046 | 0F 0A 3A000000 3F00 0000 | map leaf, size 10: @3A → @3F |
| 050 | 4C 74797065 | txt "type" |
| 055 | 6C 6E756D626572 | txt "number" |
| 05C | 0F 0A 50000000 5500 0000 | map leaf, size 10: @50 → @55 |
| 066 | 6C 706C61796572 | txt "player" |
| 06D | 0F 0A 66000000 3000 0000 | map leaf, size 10: @66 → @30 |
| 077 | 1C 79 | txt "y" |
| 079 | 0F 0A 77000000 5C00 0000 | map leaf, size 10: @77 → @5C |
| 083 | 1C 78 | txt "x" |
| 085 | 0F 0A 83000000 4600 0000 | map leaf, size 10: @83 → @46 |
| 08F | 07 12 A0040000 6D00 0000 79000000 8500 0000 | map branch, size 18, depth 0 (root), slots 5, 7, 10: @6D → @79 → @85 |
| 0A1 | AC 70726F70657274 696573 | txt "properties" |
| 0AC | 0F 0A A1000000 8F00 0000 | map leaf, size 10: @A1 → @8F |
| 0B6 | 8C 72657175697265 64 | txt "required" |
| 0BF | 0F 0A B6000000 0F00 0000 | map leaf, size 10: @B6 → @F |
| 0C9 | 14 14 616464697469 6F6E616C50726F70 657274696573 | txt "additionalProperties" |
| 0DF | 01 | bit false |
| 0E0 | 0F 0A C9000000 DF00 0000 | map leaf, size 10: @C9 → @DF |
| 0EA | 4C 74797065 | txt "type" |
| 0EF | 6C 6F626A656374 | txt "object" |
| 0F6 | 0F 0A EA000000 EF00 0000 | map leaf, size 10: @EA → @EF |
| 100 | 07 16 A8040000 AC00 0000 BF000000 E000 0000 F6000000 | map branch, size 22, depth 0 (root), slots 3, 5, 7, 10: @AC → @BF → @E0 → @F6 |
| 116 | 00010000 00000000 | root → @100; previous: none |
The SDK plugin derives the payload types and route helpers from this definition. The Events plugin validates publications before delivery, including when no subscriber is present. Invalid data reaches no subscriber. The example below imports the generated SDK.
Code
func shareCursors(game *Game, alice, bob *events.Owner) *events.Subscription[events.CheckersTableCursorData] {
subscription, err := events.SubscribeCheckersTableCursor(bob)
check(game.t, err)
cursor := events.CheckersTableCursorData{
Player: "alice",
X: 6.2,
Y: 3.1,
}
delivered, err := events.PublishCheckersTableCursor(game.db, cursor)
check(game.t, err)
if delivered.Delivered != 1 || delivered.Dropped != 0 {
game.t.Fatal("cursor delivery failed")
}
received, err := subscription.Receive(time.Second)
check(game.t, err)
if received.Data != cursor || received.Dropped != 0 || game.revision() != 0 {
game.t.Fatal("cursor changed board state")
}
return subscription
}