$ show y-crdt/pycrdt
pycrdt is a Python library providing bindings for Yrs, the Rust port of the Yjs CRDT framework. It enables real-time collaborative editing by offering shared data types (Text, Array, Map, XmlFragment, etc.) that automatically merge concurrent modifications across distributed replicas. The library implements a full transaction system, event observation, document synchronization protocols, awareness management, undo/redo functionality, snapshots, and typed containers with static type analysis support. The architecture is a hybrid Python/Rust package using PyO3 for the FFI layer, with the heavy lifting done in the `yrs` Rust crate. The Python layer provides clean, idiomatic interfaces with both synchronous and async APIs, leveraging `anyio` for async transaction and event handling. The library is well-tested with 100% code coverage, includes extensive documentation, and follows modern packaging practices with `maturin` for building and publishing. It is suitable for building collaborative applications such as real-time text editors, whiteboards, or any multi-user data-sharing system.
The LLM identified 2 files worth highlighting for reuse:
Provides a complete implementation of variable-length integer encoding (varint) and message framing for the Yjs binary protocol. Includes `Encoder` and `Decoder` classes for writing/reading length-prefixed messages, and helper functions for creating sync and awareness messages. This module is self-contained, relying only on Python standard library, and could be extracted into a standalone library for any project needing varint encoding or Yjs-compatible message serialization. Demonstrates clean error handling and efficient byte-stream processing.
Implements the Yjs awareness protocol for tracking client presence and state. Manages a dictionary of client states with clock-based ordering, provides methods for encoding/decoding awareness updates, and handles periodic timeout checks to remove stale clients. Uses `anyio` for async task management and supports subscription-based event notification. The core logic (state tracking, clock increment, timeout detection) is isolated from the document implementation and could be adapted to other collaborative frameworks with minimal changes. Good example of a clean async service pattern with proper lifecycle management (start/stop).