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:

dust35 B
player alice
x      6.2
y      3.1

Define the messages

The game data installs publish/checkers/table/cursor in the events definition folder:

dust219 B
type                 object
required             [player x y]
additionalProperties false
properties           {player {type string}
                      x      {type number}
                      y      {type number}}

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

python517 B
async def share_cursors(game, alice, bob):
    subscription = await events.subscribe_checkers_table_cursor(bob)
    cursor: events.CheckersTableCursorData = {"player": "alice", "x": 6.2, "y": 3.1}
    delivered = await events.publish_checkers_table_cursor(game.db, cursor)
    assert delivered == {"delivered": 1, "dropped": 0}
    received = await subscription.receive(WAIT)
    assert received["data"] == cursor and received["dropped"] == 0

    assert (await game.board())["revision"] == 0

    return subscription