Skip to content
Unity Client SDK

Unity Client SDK

The Unity SDK (cn.hetudb.clientsdk) is how a Unity game talks to a HeTu server. It wraps the WebSocket protocol, the message pipeline (MessagePack + zlib + crypto), and the subscription bookkeeping behind two layered entry points:

  • HeTuClient.Instance — singleton over one physical WebSocket. Bare metal: you call Connect, you handle drops, you re-auth yourself.
  • HeTuSessionClient.Instance — recommended for game clients. Singleton too. A persistent logical session built on top of HeTuClient: auto-reconnect, re-runs your bootstrap (e.g. login), and restores every live subscription on its own.

The wire protocol, components, CallSystem, and WatchRow / WatchRange shapes are identical between the two — the session client just keeps them alive across network drops. Read Connect, call, disconnect first to understand the primitives, then jump to Logical session for the production pattern.

This page assumes you can already start a server and have read Concepts — Components, Systems, and Subscriptions are mentioned without re-explanation.

Install

Add the package via UPM (Window → Package Manager → + → Add from git URL):

https://github.com/Heerozh/HeTu.git?path=/ClientSDK/unity/cn.hetudb.clientsdk

After import, open HeTu → Setup Wizard… and walk through the three steps it offers:

  1. NuGet dependencies — MessagePack, BouncyCastle.
  2. UPM dependencies — UniTask on Unity 2022.3, Awaitable is built in on Unity 6000+.

The wizard pops automatically on first import; that’s expected.

Connect, call, disconnect

The whole client is a singleton: HeTuClient.Instance. There is no separate “client builder” object — you configure callbacks on the singleton and call Connect.

using HeTu;
using UnityEngine;

public class NetBootstrap : MonoBehaviour
{
    public long SelfID = 1;

    async void Start()
    {
        // Connect() returns once the handshake is done.
        await HeTuClient.Instance.Connect("ws://127.0.0.1:2466/hetu/MyGame");

        // The socket is up — call straight away.
        await HeTuClient.Instance.CallSystem("login", SelfID);

        // Optional: be notified when the socket later drops.
        HeTuClient.Instance.OnClosed += err =>
            Debug.Log($"disconnected: {err ?? "normal close"}");
    }

    void OnDestroy() => HeTuClient.Instance.Close();
}

Key points the source enforces but isn’t always obvious from a snippet:

  • URL format — ws://<host>:<port>/hetu/<instance> (or wss://... when the reverse proxy in front, e.g. Caddy, terminates TLS; HeTu itself does not). The /hetu/ prefix is required, and <instance> must match one of the INSTANCES: entries in your server’s config.yml (or the --instance flag if it was started from CLI). An unknown instance is rejected after the handshake — by design, so port scanners can’t enumerate valid names.
  • Connect returns once the handshake is complete. A successful await means the socket is up, the encryption / dictionary handshake finished, and you can call CallSystem / WatchRow / WatchRange immediately. To observe an unsolicited disconnect, subscribe to the OnClosed event — it fires when the socket drops, with null for a clean server close or the error string otherwise (it does not fire for your own Close()). (Older versions of the SDK had Connect block until close; if you’re upgrading, replace the while (true) { await Connect... } reconnect loop with HeTuSessionClient below.)
  • Kicked (logged in elsewhere). When the same account logs in on another connection, the server closes this one with WebSocket close code 4001 (HeTuCloseCode.Kicked) and reason "kicked". Inside OnClosed, check HeTuClient.Instance.LastCloseCode == HeTuCloseCode.Kicked to tell the player “logged in elsewhere” — and don’t reconnect automatically, or you’ll log in again and kick the other device right back.
  • Rejected. In release mode, when the client breaks the rules (calls a System the server doesn’t have, lacks permission, passes the wrong number of arguments, sends a malformed message, exceeds the send-rate limit), the server closes the connection with close code 4002 (HeTuCloseCode.Rejected) and reason "rejected", without the details (see the server log). The usual cause is a client newer than the server. Don’t reconnect automatically either — the same call would get you disconnected again, over and over. Every code in 4000-4999 is terminal like this (HeTuCloseCode.IsTerminal(code)); HeTuSessionClient goes straight to Faulted on all of them (with a HeTuConnectionRejectedException, whose Code is the close code, for anything other than kicked). A server-side error (backend unreachable, a System raised) closes with 1011 (HeTuCloseCode.InternalError), which is fine to reconnect after.
  • Connect(url, authKey) is the same call but signs the handshake with a pre-shared key; use this if your server runs with --authkey.
  • One Close() per Connect(). Close() cancels in-flight CallSystem / WatchRow / WatchRange calls and tears the socket down — call it from OnDestroy so quitting Play Mode doesn’t leak a worker task.
  • No automatic reconnect at this layer. When the socket drops, HeTuClient stays down until you call Connect again. If you need a session that survives transient drops and replays its subscriptions, use HeTuSessionClient .

Calling Systems

CallSystem(name, args...) invokes a server-side System by name. You have two ways to use it:

// Fire-and-forget: returns immediately, queued, sent in order.
HeTuClient.Instance.CallSystem("move_to", x, z).Forget();

// Await: waits for the server's reply (default "ok", or whatever
// ResponseToClient(...) returned from the System).
var resp = await HeTuClient.Instance.CallSystem("buy", itemId);
Debug.Log(resp.To<string>());

Use .Forget() (or _ = CallSystem(...)) for fast input streams like per- frame movement; await for actions whose result you actually need.

Calls that wait for their pushes. CallSystem’s reply is sent as soon as the server commits, while the subscription updates caused by that write arrive about one batching interval (100 ms by default) later. If you refresh the UI from subscription data right after the call returns — say, re-enabling a button based on how much gold is left — you read the old values. Use CallSystemAwaitPush there: it returns only after the pushes caused by the call have arrived, so the objects from WatchRow / WatchRange are already up to date.

buyButton.interactable = false;
await HeTuClient.Instance.CallSystemAwaitPush("buy", itemId);
// the gold subscription already reflects the purchase here
buyButton.interactable = gold.Data.value >= price;

CallSystemAwaitPush takes about one batching interval longer than CallSystem and doesn’t hold up other calls or pushes on the same connection; keep high-frequency calls such as movement on CallSystem. Like subscriptions themselves it is best-effort: when the server is overloaded, a push can still arrive after it returns. HeTuSessionClient and the headless HeadlessHeTuClient have the same method; if the connection drops after the server’s reply arrived but before the pushes did, it returns successfully (the commit definitely happened, and subscriptions are restored on reconnect).

Local pre-callbacks. You can register a client-side hook that runs every time you call a System with that name — useful for client-side prediction:

HeTuClient.Instance.SystemLocalCallbacks["move_to"] = args =>
{
    // optimistic local update before the server round-trip
    transform.position = new Vector3((float)args[0], 0, (float)args[1]);
};

Subscriptions: WatchRow vs WatchRange vs WatchTable

All three subscriptions are live: the server pushes deltas as the underlying rows change in Redis.

Consistency note: subscription pushes are best-effort and eventually consistent. Under normal load you get the latest data in about 99% of cases (usually within 100–200 ms of the change); when the server’s Redis is overloaded, stale data may remain until that row changes again. Use subscription data for display; leave decisions that need strong consistency — spending currency, granting rewards, validation — to a server System.

APIReturnsUse it when
WatchRow<T>(index, value)RowSubscription<T> (one row, or null if no row matched)You want exactly one row by a unique key — your own HP, your own inventory record.
WatchRange<T>(index, left, right, limit, desc, force)IndexSubscription<T> (a dictionary of rows, kept in sync)You want a window over an indexed column — nearby players, top-N leaderboard, the last 100 chat messages.
WatchTable<T>()IndexSubscription<T> (the whole table, kept in sync)You want a whole “many rows, small rows, rarely change” table — all player names, the guild list, public config.

The T parameter is your strongly-typed Component class (see next section). Drop it for DictComponent, a string-keyed Dictionary you index manually.

WatchRange’s force=true (default) keeps the subscription alive even if the initial query returns zero rows, so newly-inserted rows still trigger OnInsert / ObserveAdd. Set force=false if you not want subscript an empty query.

WatchTable returns the very same IndexSubscription<T> as WatchRange, with identical events and R3 streams. The difference is on the server: WatchRange subscribes to one channel per row in the result set (thousands of rows means thousands of channels), while WatchTable listens to a single table-level channel no matter how many rows there are, so it does not count against the row-subscription quota. The trade-off is that every write to that table triggers a notification (filtered by permission before it reaches you), so keep using WatchRange for hot tables such as positions or HP. The server component must be declared with table_sub=True (see Concepts · Subscriptions ); the server refuses undeclared tables and tables larger than MAX_TABLE_SUBSCRIPTION_ROWS (100k by default), and the call resolves to null.

A table subscription reads its rows about 100 ms (one push interval) after it becomes active, so that the replica it reads from has caught up. To subscribe to several tables, send all the calls first and then await them: the server lets them wait together, whereas awaiting one at a time pays the wait once per table.

// Several tables after login: send them all, then await
var namesTask = HeTuClient.Instance.WatchTable<PlayerNames>();
var guildsTask = HeTuClient.Instance.WatchTable<GuildList>();
var names = await namesTask;
var guilds = await guildsTask;

For a WatchRange point query (left == right, e.g. WatchRange<Item>("owner", myId, myId, 100)) to be efficient, declare the server-side index with point_sub=True; otherwise any write to that index makes the server re-run the comparison.

// Every player's name: thousands of rows, one subscription
var names = await HeTuClient.Instance.WatchTable<PlayerNames>();
names.OnInsert += (sub, id) => AddNameTag(sub.Rows[id]);
names.OnUpdate += (sub, id) => RefreshNameTag(sub.Rows[id]);
names.OnDelete += (sub, id) => RemoveNameTag(id);

Watching a group of a composite index

For a composite index declared on the server with property_field(groupby=...) (see Concepts ), pass the values of every group-by column in the last parameter, group, to watch just that group. Both WatchRow and WatchRange take it:

// level grouped by zone: rows of zone 3 with level in [10, 20], ordered by level
var zone3 = await HeTuClient.Instance.WatchRange<Monster>(
    "level", 10, 20, 100,
    group: new Dictionary<string, object> { ["zone"] = 3 });

// a composite index unique within a group: one row by (guild, name)
var member = await HeTuClient.Instance.WatchRow<GuildMember>(
    "name", "Ann", group: new Dictionary<string, object> { ["guild"] = guildId });
  • Value types: group values must be integers, bools, integral floats, string or byte[]. An integer column also accepts a numeric string such as "3", the same as single-column index queries.
  • Thrown on the spot: an invalid value, or a group on id, throws ArgumentException and nothing is sent.
  • Connection closed: a misspelled group-by column, or a value of the wrong type for its column (e.g. a non-numeric string for an integer column, or a number for a string column), cannot be seen by the client; the server closes the connection with 4002, which is terminal for the session (it does not reconnect).
  • The dictionary is copied: changing it after the call does not affect the subscription, and a reconnect restores with the group given at subscribe time. WatchRow still restores by the row’s id once the row is resolved.
  • What wakes it up: only writes in its own group; writes in other groups never wake it. The server does not need point_sub for this.
  • Server version: subscriptions with group need a server that supports composite indexes (later than v2025.11.1). Older servers do not understand group and close the connection with 4002. Subscriptions without group send exactly the same frames as before.

Consuming a commit sequence column on the client

A commit sequence column declared on the server with property_field(commit_seq=True, groupby=...) (see Concepts ) is numbered from 1 per group at commit. A client can watch that group and consume it by number:

// rows of my owner group numbered after the cursor, a window of 100 rows
var log = await HeTuClient.Instance.WatchRange<ChangeLog>(
    "seq", cursor + 1, long.MaxValue, 100,
    group: new Dictionary<string, object> { ["owner"] = myId });
  • Advance the cursor only over consecutive numbers: when one push carries several rows, sort them by seq and handle them one number after another from the cursor plus one, moving the cursor as you go. Stop at a missing number and buffer the rows after it: behind a read/write splitting proxy that routes per command, or on native cluster with several replicas, the server may miss a row in the middle when it reads the batch back, and a later push fills it in. Don’t jump the cursor to the largest number in the batch; the missed row would never be handled.
  • Re-subscribe when the window is full: a subscription only pushes rows in its window (limit rows). Once you reach the end of the window, subscribe again from the new cursor and unsubscribe the old one.
  • Every row of the group must be visible to this client, e.g. an OWNER component with groupby="owner". A row hidden by RLS still takes a slot in the window but is never pushed, so the client stops in front of it forever; if that can’t be guaranteed, consume on the server instead (see “Binlog-style syncing” in Advanced ).
  • A missing number that never fills in: the client cannot tell whether a missing number is permanent (e.g. the row was deleted). Either make sure the server never deletes rows in the middle, or, when it still hasn’t filled in after a while, call a server System that confirms and skips the gap.
  • Getting the number of a row you just inserted: a row inserted in a System only gets its number at commit, so the row the System returns still has 0. The direct way is to have the System commit early and send the number back: seqs = await ctx.session_commit(), then return hetu.ResponseToClient({"seq": seqs[ChangeLog][row.id]}) (see “Binlog-style syncing” in Advanced ); the cost is that code after the commit runs outside the transaction. If you don’t want to commit early, watch the group and call with CallSystemAwaitPush: by the time it returns, the push has arrived; find the pushed row by the id the System returned to get its number. This needs the new row to fall inside the subscription’s window, e.g. watch the latest rows with desc: true, or make sure the window isn’t full; otherwise the new row is not pushed and CallSystemAwaitPush still returns, without the number.

Typed components vs DictComponent

The server can generate matching C# classes from your Component definitions via hetu build. The result implements IBaseComponent:

public class Position : IBaseComponent
{
    public long ID { get; set; }   // ID is mandatory; matches `id` on the server
    public long owner;
    public float x;
    public float y;
}

With a typed T, you read fields directly: sub.Data.x. Without it, you get a DictComponent (a Dictionary<string, object>), and you read with Convert.ToSingle(sub.Data["x"]) — flexible but verbose, and you lose compile-time field checks.

Two ways to react to data changes

The same subscription object exposes both an event-based API and an R3 reactive API. Pick whichever fits the call site — they coexist, they’re backed by the same internal state, and you can mix them in the same codebase.

Pattern A — event callbacks

Plain C# events. No extra dependencies.

async void SubscribeOthers()
{
    var players = await HeTuClient.Instance.WatchRange<Position>(
        "owner", 1, 999, 100);
    players.AddTo(gameObject); // dispose when this GameObject is destroyed

    // Initial rows are already populated:
    foreach (var p in players.Rows.Values)
        AddPlayer(p);

    // Server-side INSERT into the index range
    players.OnInsert += (sender, rowID) =>
        AddPlayer(sender.Rows[rowID]);

    // Server-side UPDATE on a row already in the range
    players.OnUpdate += (sender, rowID) =>
    {
        var p = sender.Rows[rowID];
        MovePlayer(p.owner, new Vector3(p.x, 0.5f, p.y));
    };

    // Server-side DELETE, or row leaving the range
    players.OnDelete += (sender, rowID) =>
        RemovePlayer(sender.Rows[rowID].owner);
}

For a single-row RowSubscription<T> the events are simpler: OnUpdate(sender) and OnDelete(sender).

Pattern B — R3 reactive streams

The same subscription exposes Observable<T> streams. This pays off when you’re chaining operators or binding to UI.

async void SubscribeOthers()
{
    var players = await HeTuClient.Instance.WatchRange<Position>(
        "owner", 1, 999, 100);
    players.AddTo(gameObject);

    // Add stream — initial rows are emitted first, then live inserts.
    players.ObserveAdd()
        .Subscribe(p => AddPlayer(p))
        .AddTo(ref players.DisposeBag);

    // Remove stream — emits the row ID that left the range.
    players.ObserveRemove()
        .Subscribe(rowID => RemovePlayer(rowID))
        .AddTo(ref players.DisposeBag);

    // Per-row update stream — completes (OnCompleted) when the row is removed.
    foreach (var rowID in players.Rows.Keys)
        BindRow(players, rowID);
    players.ObserveAdd().Subscribe(p => BindRow(players, p.ID))
        .AddTo(ref players.DisposeBag);
}

void BindRow(IndexSubscription<Position> players, long rowID)
{
    players.ObserveRow(rowID)
        .Subscribe(p => MovePlayer(p.owner, new Vector3(p.x, 0.5f, p.y)))
        .AddTo(ref players.DisposeBag);
}

For RowSubscription<T>, use sub.Subject — it emits the current row first, then every update, and is ideal for direct UI binding:

var hp = await HeTuClient.Instance.WatchRow<HP>("owner", SelfID);
hp.AddTo(gameObject);

hp.Subject
    .Select(x => x.ID != 0 ? $"HP: {x.value}" : "Dead")
    .SubscribeToText(hpLabel)        // R3 Unity extension
    .AddTo(ref hp.DisposeBag);

When to prefer which:

  • R3 approach is recommended, as it is more concise and clear, and involves less code.
  • Events for a couple of simple side-effects (spawn, move, despawn).

Subscription lifecycle (don’t skip this)

Every subscription holds a server-side resource. The SDK’s finalizer logs an error if a subscription is GC’d without Dispose() — that is a real leak, not a warning to ignore.

Three correct patterns:

// 1. Tie to a GameObject — disposes on Destroy.
sub.AddTo(gameObject);

// 2. Tie to a DisposableBag (for nested R3 subscriptions, or grouping).
sub.AddTo(ref _bag);

// 3. Manual.
try { /* use sub */ } finally { sub.Dispose(); }

Dispose() releases the subscription you hold: it tears down all R3 streams chained off it, and after dispose its Subject / ObserveRow streams and events stop firing.

The same row / range can be watched from several places: every WatchRow / WatchRange / WatchTable call returns its own subscription object; objects for the same query share one server subscription and the same data (Data / Rows). Dispose each on its own lifecycle without affecting the others — the SDK tells the server “stop pushing me changes for this query” only when the last one is disposed.

Two caveats:

  • A subscription that is never disposed keeps the server subscription alive until the connection closes, even if every other subscription of the same query has been disposed. The SDK’s leak log only shows up once it is collected after the disconnect, so don’t rely on it to catch leaks early.
  • The unsubscribe can go out a bit later: while subscribe requests are still awaiting replies, the SDK waits for them before unsubscribing, so a request that lands on the same subscription isn’t dropped with it. With a constant stream of subscribe requests (e.g. paging quickly), old subscriptions stay until the requests pause.

Logical session: HeTuSessionClient

A bare HeTuClient is one physical WebSocket — if it drops, your data goes silent until you reconnect, re-login, and re-issue every WatchRow / WatchRange. HeTuSessionClient does that bookkeeping for you. Like HeTuClient, it’s a singleton (HeTuSessionClient.Instance) — it owns the underlying HeTuClient.Instance, reconnects on its own, replays your bootstrap (typically the login round-trip), and re-issues every still-alive subscription using its original watch intent (component + index + value / range).

var session = HeTuSessionClient.Instance;

await session.Connect(
    "ws://127.0.0.1:2466/hetu/MyGame",
    bootstrap: async client =>
    {
        // 'client' is HeTuClient.Instance — the physical connection that's
        // being brought up. Re-auth on every (re)connect.
        await client.CallSystem("login", SelfID);
    });
// session.State is now Ready.

using var hp = await session.WatchRow<HP>("owner", SelfID);
using var nearby = await session.WatchRange<Position>("zone", zone, zone, 50);

await session.CallSystem("move_to", 12f, 8f);

Connect only resolves after bootstrap and subscription restore have both completed — so once it returns, your live data is hot and your code is on the same footing it would have been before the drop.

The full signature:

Awaitable Connect(
    string url,
    string authKey = null,
    Func<HeTuClient, Awaitable> bootstrap = null,
    TimeSpan? reconnectDelay = null,        // default: 1s
    TimeSpan? maxReconnectDelay = null,     // default: 30s (exponential cap)
    int maxReconnectAttempts = 20,          // 0 = unlimited
    TimeSpan? connectTimeout = null,        // default: 30s
    TimeSpan? requestTimeout = null,        // default: 30s
    double? reconnectJitter = null);        // default: 0.5

What the await can do:

  • resolve when the session reaches Ready — normal path.
  • throw the underlying failure exception as soon as it happens — the session does not retry before first Ready. Anything that fails the initial connect (socket close, bootstrap throw, restore error) lands in Faulted terminal state on the first attempt, and the awaiter sees the original exception (e.g. your CallSystem("login", ...) exception, or InvalidOperationException("Connection closed: <reason>")). Retries with the same URL / credentials wouldn’t help, so the SDK doesn’t try.
  • throw TimeoutException if Ready isn’t reached within connectTimeout (default 30 s) — primarily a safety net for a transport that hangs without ever raising close/error. The Message and InnerException carry the last captured fault (if any). The session is Close()’d on the way out; you can Connect(...) again afterward.
  • throw OperationCanceledException if someone called Close() from another code path.

Calling Connect while the session is already running throws — call Close() first if you really want to reconnect with a different URL. After Close() (or Faulted), you can Connect again and the state machine starts from scratch with a fresh core.

Defaults — when to override

  • reconnectDelay = 1s, maxReconnectDelay = 30s — exponential back-off from 1 s, doubling each attempt, capped at 30 s. Reasonable for both transient hiccups and longer outages; the cap stops you from hammering the server while still waking up promptly when it recovers.
  • reconnectJitter = 0.5 — each wait is a random point in [0.5, 1] × the current back-off delay (0.5–1 s at first, 15–30 s at the cap); the back-off itself still doubles as above. Clients that dropped at the same moment (server restart, gateway taking a node out) would otherwise all come back in the same instant, attempt after attempt, and every wave is another connection storm. Pass 0 for exact delays.
  • maxReconnectAttempts = 20 — only governs reconnects after the session has reached Ready at least once. With the exponential schedule above, 20 attempts spans roughly 4–8 minutes (1+2+4+8+16+30 × 15 ≈ 481 s before jitter) of in-game retry before surrendering to Faulted. Long maintenance windows: pass 0 (unlimited) so a player can leave the app open and rejoin when the server returns. Initial connect always uses 1 attempt — there’s no retry value when the credentials or URL might be wrong.
  • connectTimeout = 30s — guards the initial Connect only; reconnects after Ready aren’t subject to it. If you set maxReconnectAttempts > 0, consider raising or disabling this — otherwise whichever budget runs out first wins.

State machine

session.State walks these in order, and StateChanged fires on every transition:

StateMeaning
StoppedInitial, or after Close() / Dispose(). Connect() moves it forward.
ConnectingWebSocket handshake in progress.
BootstrappingSocket up; your bootstrap delegate is running.
RestoringSubscriptionsBootstrap done; re-issuing each live WatchRow / WatchRange.
ReadyQueued calls have been flushed; the Ready event fires. Steady state.
ReconnectingTransient drop; waiting out the current back-off delay (with jitter), then back to Connecting.
FaultedTerminal — either an initial-connect failure (always terminal), post-Ready maxReconnectAttempts reached, kicked (HeTuKickedException, never retried), or another terminal close code such as rejected (HeTuConnectionRejectedException, never retried). Faulted event carries the last Exception.

The Faulted event is broader than the Faulted state: it fires once per failed attempt — pre-Ready failures fire it once then go terminal; post-Ready transient drops fire it on every retry. Check session.State from inside the handler to tell terminal from transient (post-Ready retry keeps state in Reconnecting).

Being kicked is the exception to retrying: when the account logs in elsewhere, the server closes the connection with close code 4001, and the session goes straight to Faulted with a HeTuKickedException instead of reconnecting — a reconnect would re-run your login bootstrap and kick the other device, and the two would keep kicking each other. Check for it in the Faulted handler (or around await Connect) to show “logged in elsewhere”. Any other terminal close code (4000-4999, e.g. 4002 rejected) likewise goes straight to Faulted, with a HeTuConnectionRejectedException: retrying the same request would only be rejected again. Show something like “version mismatch, please update”.

CallSystem semantics

Calls behave differently depending on when a drop hits them:

  • Not yet sent (queued before Ready) — held until the session is Ready, then dispatched in order. Safe to issue from anywhere, including from inside the Ready event.
  • In-flight when the socket dropped — fail with CallOutcomeUnknownException. The session never auto-retries these, because the server may already have applied the call’s side-effects. If the call is idempotent, catch and retry it yourself.
try {
    await session.CallSystem("buy", itemId);
} catch (CallOutcomeUnknownException) {
    // Server may or may not have charged the player. Decide per-call.
}

Subscription survival

session.WatchRow<T> / session.WatchRange<T> return the same RowSubscription<T> / IndexSubscription<T> you’ve seen above, with three extra hooks the session populates:

  • sub.IsStale — true while the underlying socket is down. sub.Data / sub.Rows keep their last-known values; no updates are coming until the session is Ready again.
  • sub.OnResynced — fires once after each reconnect, after the snapshot has been refreshed. Any inserts / updates / deletes that happened during the drop are replayed first through the normal OnInsert / OnUpdate / OnDelete events (and the matching R3 streams), so your handlers see a clean diff — not just a “everything reloaded” signal. Use it to clear any “stale” indicators in your UI.
  • De-duplication — calling WatchRow / WatchRange twice with the same arguments sends one server subscription but hands each caller its own subscription object; the remote unsubscribe only happens when every one is disposed, and a reconnect restores the query once for all of them.

RowSubscription’s restore target is the bound row’s ID, not the original index/value: if your call was session.WatchRow<Player>("owner", userId) and the matched row’s primary key is 42, reconnects re-subscribe by id=42. A row that stops matching the original predicate while you were offline is not swapped out — the subscription fires Update(null) and stays bound to 42.

Shutdown

HeTuSessionClient.Instance.Close() (or Dispose() — they’re aliases) cancels in-flight calls with OperationCanceledException, disposes every subscription it owns, and tears the socket down. The singleton itself survives — call Connect(...) again to start a fresh session.

async void Start() {
    await HeTuSessionClient.Instance.Connect(url, bootstrap: Login);
    // ... watch / call ...
}

void OnDestroy() => HeTuSessionClient.Instance.Close();

Unity version notes

  • Unity 6000+ — Connect, CallSystem, WatchRow, and WatchRange return Awaitable<T>. Use await Awaitable.WaitForSecondsAsync(...) for delays.
  • Unity 2022.3 — same APIs return UniTask<T>. Install UniTask through the Setup Wizard. Use await UniTask.Delay(ms) for delays.

Both code paths are compiled behind #if UNITY_6000_0_OR_NEWER, so your calling code only needs to choose one delay style.

Where to next

  • Advanced — System copies, scheduled future calls, raw Endpoints, custom pipeline layers, and the engine internals you’ll reach for once a project gets real.
  • Concepts — re-read the Subscriptions section now that you’ve seen the client side; permissions / RLS filter what WatchRow and WatchRange can return.
  • Tutorial: Chat Room — a complete client-and- server example using the patterns above.