awareness

package
v1.57.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 10 Imported by: 2 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package awareness implements the Yjs awareness protocol for ephemeral state such as user presence, cursor positions, and selections.

Awareness state is not persisted, not replayed on reconnect, and expires after a period of inactivity. It is separate from document updates.

Reference: https://github.com/yjs/y-protocols/blob/master/awareness.js

Quick start

a := awareness.New(clientID)
a.SetLocalState(map[string]any{"name": "Alice", "cursor": 42})
a.OnChange(func(evt awareness.ChangeEvent) {
    // react to added/updated/removed peers
})
update := a.EncodeUpdate(nil)
// send update to peers; peers call a.ApplyUpdate(update, origin)

See the Example* functions for canonical usage patterns.

Stability

ygo follows semantic versioning. The v1.x public API is considered stable: new functionality lands as minor releases; bug fixes as patch releases; breaking changes are deferred to v2.

Index

Examples

Constants

View Source
const DefaultTimeout = 30 * time.Second

Variables

View Source
var ErrStateTooLarge = errors.New("awareness: client state exceeds maximum size")

ErrStateTooLarge is returned when a single client state exceeds maxAwarenessStateBytes.

View Source
var ErrTooManyClients = errors.New("awareness: update exceeds maximum client count")

ErrTooManyClients is returned when an update claims more clients than maxAwarenessClients.

Functions

This section is empty.

Types

type AdmitEntry added in v1.55.0

type AdmitEntry struct {
	ClientID uint64
	// WasLive and Live report whether the client holds state before and after
	// the entry.
	WasLive, Live bool
}

AdmitEntry describes an update entry that ApplyUpdateWith is about to apply.

type ApplyOptions added in v1.55.0

type ApplyOptions struct {
	// Foreign marks presence relayed from another node, subject to
	// SetMaxForeignClients.
	Foreign bool
	// Admit, when set, vets each entry about to be applied, after the clock
	// gate and the caps; returning false refuses the entry. It runs with the
	// Awareness lock held, so it sees the outcome the entry will really have,
	// and must not call Awareness methods. isLive reports whether a client is
	// live at that point and is valid only during the call.
	Admit func(e AdmitEntry, isLive func(clientID uint64) bool) bool
}

ApplyOptions adjusts how ApplyUpdateWith applies an update.

type ApplyResult added in v1.55.0

type ApplyResult struct {
	// Applied lists the clients the update added, updated or removed; an ID
	// the update names more than once may repeat.
	Applied []uint64
	// Refused reports whether a cap or Admit refused any entry.
	Refused bool
	// Altered reports whether any other entry was not applied as sent: it was
	// discarded as stale, its state was dropped as invalid or over a cap, it
	// removed a client that was not live or was not spelled null, or it was a
	// null for the local client. When neither this nor Refused is set, every
	// entry is a live state the room now holds or a null that retired one.
	Altered bool
}

ApplyResult reports what ApplyUpdateWith did.

type Awareness

type Awareness struct {
	// contains filtered or unexported fields
}

Awareness tracks ephemeral peer state.

func New

func New(clientID uint64) *Awareness

New creates an Awareness instance for the given client.

func (*Awareness) ApplyUpdate

func (a *Awareness) ApplyUpdate(update []byte, origin any) error

ApplyUpdate decodes an incoming awareness update and merges it. Only updates with a higher clock than the current one are applied. Returns ErrTooManyClients if the update claims more than maxAwarenessClients entries, or ErrStateTooLarge if any single state JSON exceeds maxAwarenessStateBytes.

func (*Awareness) ApplyUpdateContext added in v1.6.0

func (a *Awareness) ApplyUpdateContext(ctx context.Context, update []byte, origin any) error

ApplyUpdateContext is the context-aware variant of ApplyUpdate. If ctx is already cancelled, the update is NOT applied and ctx.Err() is returned. Otherwise the update is applied and the underlying ApplyUpdate's error (or nil) is returned.

func (*Awareness) ApplyUpdateWith added in v1.55.0

func (a *Awareness) ApplyUpdateWith(update []byte, origin any, opts ApplyOptions) (ApplyResult, error)

ApplyUpdateWith is ApplyUpdate with options, reporting what it applied.

func (*Awareness) ClientID

func (a *Awareness) ClientID() uint64

ClientID returns the local client ID.

func (*Awareness) Destroy

func (a *Awareness) Destroy()

Destroy stops the auto-expiry goroutine (if started) and releases associated resources. Safe to call more than once.

func (*Awareness) EncodeUpdate

func (a *Awareness) EncodeUpdate(clientIDs []uint64) []byte

EncodeUpdate encodes the current state of the given client IDs into a binary awareness update message. Pass nil to encode all known clients, including those with a nil (removed) State, which encodes as JSON null and signals removal to peers.

func (*Awareness) GetLocalState

func (a *Awareness) GetLocalState() map[string]any

GetLocalState returns the local client's current state (nil if not set or removed).

func (*Awareness) GetStates

func (a *Awareness) GetStates() map[uint64]ClientState

GetStates returns a snapshot of all known active client states. Removed clients (State == nil) are excluded.

func (*Awareness) Heartbeat added in v1.11.0

func (a *Awareness) Heartbeat()

Heartbeat re-emits the local client's current state at an incremented clock so peers learn that we're still alive even when the state hasn't changed. No-op when no local state is set or when the local client has been removed (state == nil).

Typical use is to schedule periodic calls alongside StartAutoExpiry on peers — they expire clients that go quiet, this advertises that we haven't. Matches Yjs JS's constructor interval which re-emits local state every outdatedTimeout/2.

OnUpdate observers ARE fired (the clock advanced); OnChange observers are not (the state content is unchanged). Matches Yjs, whose interval re-emits local state via setLocalState and emits `update` but not `change`.

Added in v1.11.0 (#73 vector C5).

func (*Awareness) Meta added in v1.38.0

func (a *Awareness) Meta(clientID uint64) (ClientMeta, bool)

Meta returns the clock and last-updated time for a known client, including remote tombstones (Yjs/yrs retain meta for removed clients). LastUpdated is the last-applied time for an active client, or the tombstone time for a remote tombstone; a local tombstone (our own leaving state) has a valid Clock with a zero LastUpdated. Returns ok=false only for never-seen clients.

func (*Awareness) OnChange

func (a *Awareness) OnChange(fn func(ChangeEvent)) func()

OnChange registers a callback invoked whenever client state content changes (added, removed, or content-differing update). For an event on every applied entry including heartbeats, use OnUpdate. Returns an unsubscribe function.

func (*Awareness) OnUpdate added in v1.38.0

func (a *Awareness) OnUpdate(fn func(UpdateEvent)) func()

OnUpdate registers a callback invoked on every applied awareness entry, including heartbeats (where OnChange does not fire). Returns an unsubscribe function. Observers are not cleared by Destroy (matches OnChange).

func (*Awareness) PurgeTombstones added in v1.34.0

func (a *Awareness) PurgeTombstones(grace time.Duration) int

PurgeTombstones reclaims removal tombstones (clients with a null State) that became tombstones longer than grace ago, freeing both their slot against SetMaxClients and their memory. Without it, a high-churn room accumulates tombstones without bound — each retained entry counts toward the client cap, so the room can eventually refuse new, legitimate clients — and every full EncodeUpdate(nil) keeps re-broadcasting them as null.

grace MUST exceed the RemoveExpired timeout (and the provider's full-state re-broadcast interval): a tombstone's clock is what encodes removals and rejects stale re-adds, so it must outlive normal update reordering before it is forgotten. A non-positive grace is a no-op. The local client is never purged. Purging fires no observer event — a purged tombstone was either already signalled removed or never signalled present, so no consumer is tracking it (GetStates already hides tombstones).

Cross-peer note: after a tombstone is forgotten, a delayed low-clock update for that id is accepted as a new client, whereas a peer that has not yet purged would drop it. This transient divergence self-heals on the next broadcast and matches y-protocols' "forget after outdatedTimeout" semantics; awareness is best-effort presence, not a convergent CRDT.

Returns the number of tombstones reclaimed.

func (*Awareness) RemoveExpired

func (a *Awareness) RemoveExpired(timeout time.Duration)

RemoveExpired removes clients whose last update is older than timeout. Only active clients (with non-nil State) are tracked in meta and can expire.

The local client is exempt: peers can't reliably tell whether we've gone silent, so it's our job to broadcast presence via SetLocalState or Heartbeat. Self-expiry would create false offline signals on quiet rooms. Matches y-protocols / yrs behavior (#73 vector C4).

Expiring a client converts it to a retained null-state tombstone (its clock is kept for removal encoding and the clock gate); it does NOT free the entry. Callers driving expiry manually should pair this with PurgeTombstones to reclaim aged tombstones, or use StartAutoExpiry, which runs both stages.

func (*Awareness) SetLocalState

func (a *Awareness) SetLocalState(state map[string]any)

SetLocalState updates the local client's state and increments the clock. Passing nil removes the local client from the awareness set.

Example

ExampleAwareness_SetLocalState shows broadcasting presence state (cursor position, user color, etc.) to peers.

package main

import (
	"fmt"

	"github.com/reearth/ygo/awareness"
)

func main() {
	a := awareness.New(42)
	a.SetLocalState(map[string]any{
		"name": "Alice",
		"cursor": map[string]any{
			"line": 10,
			"col":  3,
		},
	})

	state := a.GetLocalState()
	fmt.Println("name:", state["name"])
}
Output:
name: Alice

func (*Awareness) SetLocalStateContext added in v1.6.0

func (a *Awareness) SetLocalStateContext(ctx context.Context, state map[string]any) error

SetLocalStateContext is the context-aware variant of SetLocalState. If ctx is already cancelled, the state is NOT updated and ctx.Err() is returned. Otherwise the state is updated and nil is returned.

Like TransactContext (see crdt.Doc.TransactContext), mid-call ctx cancellation is not interrupted — the operation itself is short and uninterruptible. This method provides cooperative entry-point cancellation only.

func (*Awareness) SetMaxBytes added in v1.8.1

func (a *Awareness) SetMaxBytes(n int64)

SetMaxBytes caps the cumulative byte size of awareness state across all remote clients merged via ApplyUpdate. Incoming entries that would push the total past the cap are silently dropped (treated as null) rather than rejected with an error — matches the existing pattern for oversized-state handling so a single misbehaving peer cannot cause an entire room's awareness merge to fail.

A value of 0 (the default) disables the cap. Local state set via SetLocalState is not counted; the cap is intended to constrain untrusted wire input only. See issue #48 vector B.

Set this once at construction time; changes while concurrent updates are being processed are safe (the field is read under a.mu).

func (*Awareness) SetMaxClients added in v1.25.0

func (a *Awareness) SetMaxClients(n int)

SetMaxClients caps the number of distinct client entries this Awareness will track — live presence plus retained removal tombstones. Once the cap is reached, a previously-unseen client asserting state takes the slot of the oldest remote tombstone and is refused only when there is none, while an unseen client's null is refused; clients already tracked can still update and be removed. This bounds memory against a peer that invents unbounded client IDs to exhaust the room — including null-state entries, which are NOT subject to the byte cap (see SetMaxBytes) and so would otherwise grow the states map without limit — without letting removal churn lock live clients out. A reclaimed tombstone's clock is forgotten early, with the trade-off PurgeTombstones describes, and this happens with or without StartAutoExpiry.

A value of 0 (the default) disables the cap. Local state set via SetLocalState is exempt — the local client is always allowed. Suggested production value: a few thousand to tens of thousands per room.

Set this once at construction time; changes while concurrent updates are being processed are safe (the field is read under a.mu).

func (*Awareness) SetMaxForeignClients added in v1.55.0

func (a *Awareness) SetMaxForeignClients(n int)

SetMaxForeignClients bounds the live remote entries that relayed presence (ApplyUpdateWith with ApplyOptions.Foreign) may hold, so it cannot crowd out local clients under SetMaxClients. Once reached, relayed entries for client IDs that are not currently live are refused, whether new or removed and returning; live clients still update and can be removed. It does not restrict other updates.

n is a threshold on ALL live entries, not a quota for relayed ones: entries admitted through ApplyUpdate count toward it too, so local presence filling the room shuts relayed presence out sooner than a true per-origin quota would.

Unlike SetMaxClients it counts live entries only, not removal tombstones: tombstones are reclaimed only while StartAutoExpiry runs, so counting them would let ordinary churn saturate the sub-cap permanently.

A value of 0 (the default) disables the sub-cap.

func (*Awareness) StartAutoExpiry

func (a *Awareness) StartAutoExpiry(timeout time.Duration) func()

StartAutoExpiry starts a background goroutine that periodically runs both awareness-cleanup stages: RemoveExpired(timeout) to tombstone silent clients, then PurgeTombstones(2*timeout) to reclaim tombstones whose removal has aged out (so a high-churn room does not accumulate tombstones against the client cap). The goroutine ticks at timeout/2 so that clients are expired within one tick period after their deadline. The returned function stops the goroutine; it must be called to avoid a goroutine leak. The stop function is also stored internally and will be called by Destroy.

type ChangeEvent

type ChangeEvent struct {
	Added   []uint64 // client IDs newly seen
	Updated []uint64 // client IDs whose state changed
	Removed []uint64 // client IDs whose state was set to null
	Origin  any
}

ChangeEvent is delivered to observers when states change.

type ClientMeta added in v1.38.0

type ClientMeta struct {
	Clock       uint64
	LastUpdated time.Time
}

ClientMeta is the per-client metadata returned by Meta.

type ClientState

type ClientState struct {
	Clock uint64
	State map[string]any // nil means the client was removed
}

ClientState holds the clock and decoded state for one peer.

type UpdateEvent added in v1.38.0

type UpdateEvent struct {
	Added   []uint64
	Updated []uint64
	Removed []uint64
	Origin  any
}

UpdateEvent is delivered to OnUpdate observers on every applied awareness entry, including same-content heartbeat re-emits at a bumped clock. Same shape as ChangeEvent; a distinct type prevents accidentally crossing the two callback kinds.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL