Documentation
¶
Overview ¶
Package client is a Yjs sync provider over WebSocket: the Go equivalent of y-websocket's WebsocketProvider, compatible with yserve, Hocuspocus, and the reference y-websocket server.
A Client owns a live connection for one document: it runs the y-protocols handshake (SyncStep1 / SyncStep2), broadcasts local transactions as incremental SyncUpdate frames, applies remote frames to the local Doc, exchanges awareness state, and reconnects with exponential backoff until closed.
Local edits made through the Doc between (and during) connections are never lost: each (re)connect handshake diffs against the server's state vector, so offline edits flow up and missed remote edits flow down. This is the embeddable building block for bots, CLI tools, server-side agents, and the gomobile bindings.
Concurrency: while a Client is connected, remote updates commit on background goroutines. Read document state under a read transaction (Doc.ReadTxn) and resolve root types via Doc.Branch OUTSIDE any open transaction; the shared-type wrappers' lock-free read methods are only safe when the caller holds a transaction.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a live sync session for one document. Construct with New, start with Connect, stop with Close. Safe for concurrent use.
func (*Client) Close ¶
Close stops the connection loop, removes the change observer, and closes the connection. Safe to call more than once and from multiple goroutines; the teardown runs exactly once.
func (*Client) Connect ¶
Connect starts the connection loop and returns immediately. The loop dials, handshakes, relays updates, and reconnects with backoff until ctx is cancelled or Close is called. Calling Connect twice is an error.
func (*Client) RemoveAwarenessState ¶
func (c *Client) RemoveAwarenessState()
RemoveAwarenessState clears the local awareness state (peers see the client leave) and broadcasts the removal.
func (*Client) SetAwarenessState ¶
SetAwarenessState sets the local awareness state (a JSON blob, the same convention y-protocols uses) and broadcasts it.
type Options ¶
type Options struct {
// URL is the server base, e.g. "ws://localhost:8080" or
// "wss://collab.example.com". The document name is appended as
// the URL path (y-websocket convention).
URL string
// DocName addresses the document on the server.
DocName string
// Doc is the local document the client syncs. When nil a fresh
// Doc is created; retrieve it via Client.Doc.
Doc *doc.Doc
// Awareness optionally carries per-client ephemeral state
// (cursor, name). When nil a fresh Awareness bound to the Doc's
// clientID is created.
Awareness *awareness.Awareness
// OnSynced fires on synced-state transitions: true after each
// completed handshake, false on every disconnect. Called from the
// client's goroutines; keep it fast and do not call back into
// blocking Client methods.
OnSynced func(synced bool)
// OnError observes non-fatal connection errors (dial failures,
// dropped connections). The client keeps reconnecting regardless.
//
// OnError and OnSynced may be invoked concurrently from different
// goroutines (e.g. a local-store write failure overlapping a dial
// failure), so a callback that mutates shared state must guard it.
OnError func(err error)
// MinBackoff / MaxBackoff bound the reconnect backoff. Defaults:
// 250ms / 10s.
MinBackoff time.Duration
MaxBackoff time.Duration
// LocalStore, when set, makes the client offline-first: on Connect
// the document loads its persisted state from the store (so it is
// usable before any network), every subsequent change (local edit
// or applied remote update) is persisted, and on reconnect the
// handshake carries any edits made while offline up to the server.
// Keyed by DocName. A pure-Go on-device sqlite store
// (persist/sqlite) is the typical mobile choice. nil disables
// local persistence; the document lives only in memory.
LocalStore persist.Store
}
Options configures a Client. URL and DocName are required; the zero value of every other field selects a sensible default.