Go bindings to the Rust Yrs library that implements the Yjs CRDT
README.md

Yjs Document Loader Example #

This example demonstrates how to load a Yjs document from a binary file containing V1 encoded Yjs state.

Overview #

The example shows:

  1. Reading a binary file from the filesystem
  2. Loading V1 encoded Yjs document state
  3. Creating a Yjs document from that state
  4. Reading and displaying the document contents

Usage #

Building #

cd examples
go build -o load_document .

Running #

# If the file doesn't exist, the example will create a sample document
./load_document

# Or specify your own Yjs file
./load_document /path/to/your/document.yjs

Key Functions #

UpdateFromBytes() #

This function creates a Yjs Update from raw binary data:

data, err := os.ReadFile("document.yjs")
if err != nil {
    return err
}

update := yjs.UpdateFromBytes(data)

ApplyUpdate() #

Apply the update to a document:

txn := doc.WriteTransaction()
err := txn.ApplyUpdate(update)
txn.Commit()

File Format #

The binary file should contain V1 encoded Yjs document state. This is the standard format used by Yjs/Yrs for:

  • Document persistence
  • Network synchronization
  • State snapshots

Example Output #

Loaded 247 bytes from document.yjs
✓ Successfully loaded document from binary file
Document ID: 123456789
Document GUID: abcdef12-3456-7890-abcd-ef1234567890
Text field "content": Hello from Yjs!
Array "items" length: 3
Map "metadata" entries: 0

Creating Your Own Document Files #

You can create Yjs binary files using:

  • This library (see createSampleDocument() in the code)
  • The Yjs JavaScript library
  • The Yrs Rust library directly

Notes #

  • The document will be loaded with a new random ClientID each time
  • To preserve the original ClientID, you would need to save that separately
  • The binary format is compatible across Yjs (JavaScript), Yrs (Rust), and ygo (Go)