Checkers with the events plugin

Alice and Bob share a checkers board. A confirmed move must survive a restart, but a hand cursor only matters while someone watches it. Turn notifications and animation cues help the other player follow the game. The board remains the authority when a player misses a cue.

This tutorial connects those behaviors in one program, with a version for each high-level binding. Each step adds a different event exchange to the same game. Start with the quickstart if you have not opened a database or run a stored mutation.

Read the Go example, Rust example, Clojure example, Odin example or Python example. The next step also has language and document-format selectors.

The board

Starting position

Squares are the 32 playable dark squares, numbered 0–31 from top to bottom and left to right. Red moves toward larger square numbers. Black moves toward smaller numbers. No piece is a king in this position.

The game data starts with this board:

TRON556 B
Offset (hex)Bytes (hex)Meaning
00054 52 4F 4Emagic TRON
0044C 6B696E67txt "king"
00901bit false
00A0F 0A 04000000 0900 0000map leaf, size 10: @4 → @9
0146C 737175617265txt "square"
01B02 0B000000000000 00i64 11
0240F 0A 14000000 1B00 0000map leaf, size 10: @14 → @1B
02E5C 636F6C6F72txt "color"
0343C 726564txt "red"
0380F 0A 2E000000 3400 0000map leaf, size 10: @2E → @34
04207 12 08030000 0A00 0000 24000000 3800 0000map branch, size 18, depth 0 (root), slots 3, 8, 9: @A → @24 → @38
0544C 6B696E67txt "king"
05901bit false
05A0F 0A 54000000 5900 0000map leaf, size 10: @54 → @59
0646C 737175617265txt "square"
06B02 10000000000000 00i64 16
0740F 0A 64000000 6B00 0000map leaf, size 10: @64 → @6B
07E5C 636F6C6F72txt "color"
0843C 726564txt "red"
0880F 0A 7E000000 8400 0000map leaf, size 10: @7E → @84
09207 12 08030000 5A00 0000 74000000 8800 0000map branch, size 18, depth 0 (root), slots 3, 8, 9: @5A → @74 → @88
0A44C 6B696E67txt "king"
0A901bit false
0AA0F 0A A4000000 A900 0000map leaf, size 10: @A4 → @A9
0B46C 737175617265txt "square"
0BB02 08000000000000 00i64 8
0C40F 0A B4000000 BB00 0000map leaf, size 10: @B4 → @BB
0CE5C 636F6C6F72txt "color"
0D43C 726564txt "red"
0D80F 0A CE000000 D400 0000map leaf, size 10: @CE → @D4
0E207 12 08030000 AA00 0000 C4000000 D800 0000map branch, size 18, depth 0 (root), slots 3, 8, 9: @AA → @C4 → @D8
0F44C 6B696E67txt "king"
Show all 64 nodes
Offset (hex)Bytes (hex)Meaning
0F901bit false
0FA0F 0A F4000000 F900 0000map leaf, size 10: @F4 → @F9
1046C 737175617265txt "square"
10B02 14000000000000 00i64 20
1140F 0A 04010000 0B01 0000map leaf, size 10: @104 → @10B
11E5C 636F6C6F72txt "color"
1245C 626C61636Btxt "black"
12A0F 0A 1E010000 2401 0000map leaf, size 10: @11E → @124
13407 12 08030000 FA00 0000 14010000 2A01 0000map branch, size 18, depth 0 (root), slots 3, 8, 9: @FA → @114 → @12A
1464C 72656433txt "red3"
14B0F 0A 46010000 E200 0000map leaf, size 10: @146 → @E2
1554C 72656437txt "red7"
15A0F 0A 55010000 4200 0000map leaf, size 10: @155 → @42
1644C 72656432txt "red2"
1690F 0A 64010000 9200 0000map leaf, size 10: @164 → @92
1736C 626C61636B31txt "black1"
17A0F 0A 73010000 3401 0000map leaf, size 10: @173 → @134
18407 16 1A010000 4B01 0000 5A010000 6901 0000 7A010000map branch, size 22, depth 0 (root), slots 1, 3, 4, 8: @14B → @15A → @169 → @17A
19A4C 67616D65txt "game"
19F8C 636865636B6572 73txt "checkers"
1A80F 0A 9A010000 9F01 0000map leaf, size 10: @19A → @19F
1B28C 7265766973696F 6Etxt "revision"
1BB02 00000000000000 00i64 0
1C40F 0A B2010000 BB01 0000map leaf, size 10: @1B2 → @1BB
1CE07 0E 20010000 A801 0000 C4010000map branch, size 14, depth 1, slots 5, 8: @1A8 → @1C4
1DC6C 706965636573txt "pieces"
1E30F 0A DC010000 8401 0000map leaf, size 10: @1DC → @184
1ED4C 7475726Etxt "turn"
1F23C 726564txt "red"
1F60F 0A ED010000 F201 0000map leaf, size 10: @1ED → @1F2
20007 12 00850000 CE01 0000 E3010000 F601 0000map branch, size 18, depth 0 (root), slots 8, 10, 15: @1CE → @1E3 → @1F6
2127C 235F7461626C65txt "#_table"
21A0F 0A 12020000 0002 0000map leaf, size 10: @212 → @200
Offset (hex)Bytes (hex)Meaning

Red plays 11 → 15. Black then jumps 20 → 13 → 4, capturing red2 on 16 and red3 on 8. Each turn advances the board revision once. Pieces are a map keyed by piece ID, while a jump path is an ordered sequence.

The checkers/board query reads the current board. Both mutations check the expected revision, turn and complete position before they write. A rejected or repeated turn leaves the board unchanged.

Load the plugins

Follow Use a release to configure your binding, library and license. Keep the extracted release directory layout.

Open the database with both events and sdk enabled:

go347 B
func openCheckers(path, sdkDirectory string) (*stardust.Database, error) {
	return stardust.Open(path, stardust.Options{
		Plugins: map[string]any{
			"events": map[string]any{},
			"sdk": map[string]any{
				"go":       map[string]any{"dir": sdkDirectory},
				"debounce": 250 * time.Millisecond,
				"maxWait":  2 * time.Second,
			},
		},
	})
}

Pass the database path and a dedicated SDK output directory to this function. A relative directory is relative to the process working directory. The SDK plugin replaces the output directory's contents. Keep your application source and database elsewhere. It writes typed APIs automatically at open and after stored queries, mutations or event schemas change. The debounce setting groups changes after 250 milliseconds without another definition commit. The maxWait setting limits this scheduling delay to two seconds after the first change. See the SDK reference for generation status and package imports.

The release includes generated SDK sources for these stored definitions. Each example imports those sources and uses their typed publishing, subscription, caller and worker helpers. Its database and SDK output use temporary directories that it removes when it ends. For your application, choose persistent paths. Generate the SDK after installing your definitions, then import its package in your application.

Follow the game

StepBehaviorWhat stays in the database
Pub/subShare hand cursors and announce turnsNo cursor or notification messages
RequestSubmit one move and receive one resultThe accepted move and new turn
PullReceive legal landings as a streamNo exchange messages
PushSubmit the ordered jumps of one turnThe complete accepted turn
DuplexExchange drag positions and move previewsNo draft positions

The example starts in the middle of a game and checks two specific turns. Its game data contains legal candidates and stored mutations for those positions. This keeps the code focused on event coordination. A complete game would replace those definitions with its checkers rules.

Alice, Bob and the rules worker run inside one process and share one open database. The program seeds the board and stores definitions before creating their owners. An owner groups temporary coordination resources for cleanup. The example alternates their operations so you can see both ends of each exchange. A browser client would need an application transport to reach this process.

Files in the release

After extracting the release, use these paths relative to its root:

  • Program: abi/examples/checkers/go/checkers_test.go
  • Generated SDK: abi/examples/checkers/go/gen
  • Game data: abi/examples/checkers/checkers.dust

The game data file supplies the starting board, legal-move candidates, stored queries, mutations, event schemas and expected final board. The program reads it to prepare and verify the example.

Run the example

Run from the extracted release root in a POSIX shell. These commands read the DUST game data included in the release. The page format selector changes the displayed values. The program checks each exchange and prints the final board in DUST.

sh127 B
cd abi/examples/checkers/go
GOWORK=off CGO_ENABLED=1 CHECKERS_FIXTURE=../checkers.dust CHECKERS_FORMAT=dust go test -count=1 -v

Share hand cursors to begin the exchanges.