|
| 1 | +# Loro Protocol Monorepo |
| 2 | + |
| 3 | +loro-protocol is a small, transport-agnostic syncing protocol for collaborative CRDT documents. This repo hosts the protocol implementation, a WebSocket client, and minimal servers for local testing or self‑hosting. |
| 4 | + |
| 5 | +- Protocol: multiplex multiple rooms on one connection, 256 KiB max per message, large update fragmentation supported |
| 6 | +- CRDTs: Loro document, Loro ephemeral store; extensible (e.g., Yjs, Yjs Awareness) |
| 7 | +- Transports: WebSocket or any integrity-preserving transport (e.g., WebRTC) |
| 8 | + |
| 9 | +See `protocol.md` for the full wire spec. |
| 10 | + |
| 11 | +## Packages |
| 12 | + |
| 13 | +- `packages/loro-protocol` (MIT): Core TypeScript definitions, encoders/decoders for the wire protocol |
| 14 | +- `packages/loro-websocket` (MIT): WebSocket client + a SimpleServer for local testing |
| 15 | +- `packages/loro-adaptors` (MIT): Shared CRDT adaptors for Loro documents and ephemeral state |
| 16 | + |
| 17 | +Rust workspace (AGPL): |
| 18 | + |
| 19 | +- `rust/loro-protocol`: Rust encoder/decoder mirroring the TS implementation |
| 20 | +- `rust/loro-websocket-client`: Async WS client for the protocol |
| 21 | +- `rust/loro-websocket-server`: Minimal async WS server with optional SQLite snapshotting |
| 22 | + |
| 23 | +## Quick Start (Local) |
| 24 | + |
| 25 | +Use the minimal WebSocket server for local development and tests. |
| 26 | + |
| 27 | +1. Install dependencies |
| 28 | + |
| 29 | +```bash |
| 30 | +pnpm install |
| 31 | +pnpm -r build |
| 32 | +``` |
| 33 | + |
| 34 | +2. Start a SimpleServer (Node.js) |
| 35 | + |
| 36 | +Create `examples/simple-server.ts`: |
| 37 | + |
| 38 | +```ts |
| 39 | +import { SimpleServer } from "loro-websocket/server"; |
| 40 | + |
| 41 | +const server = new SimpleServer({ port: 8787 }); |
| 42 | +server.start().then(() => { |
| 43 | + // eslint-disable-next-line no-console |
| 44 | + console.log("SimpleServer listening on ws://localhost:8787"); |
| 45 | +}); |
| 46 | +``` |
| 47 | + |
| 48 | +Run it (Node 18+): |
| 49 | + |
| 50 | +```bash |
| 51 | +node --loader ts-node/esm examples/simple-server.ts |
| 52 | +# or compile to JS first with your preferred setup |
| 53 | +``` |
| 54 | + |
| 55 | +3. Connect a client and sync a Loro document |
| 56 | + |
| 57 | +```ts |
| 58 | +// examples/client.ts |
| 59 | +import { LoroWebsocketClient, createLoroAdaptor } from "loro-websocket"; |
| 60 | + |
| 61 | +// In Node, provide a WebSocket implementation |
| 62 | +import { WebSocket } from "ws"; |
| 63 | +(globalThis as any).WebSocket = WebSocket; |
| 64 | + |
| 65 | +const client = new LoroWebsocketClient({ url: "ws://localhost:8787" }); |
| 66 | +await client.waitConnected(); |
| 67 | + |
| 68 | +const adaptor = createLoroAdaptor({ peerId: 1 }); |
| 69 | +const room = await client.join({ roomId: "demo-room", crdtAdaptor: adaptor }); |
| 70 | + |
| 71 | +// Edit the shared doc |
| 72 | +const text = adaptor.getDoc().getText("content"); |
| 73 | +text.insert(0, "Hello, Loro!"); |
| 74 | +adaptor.getDoc().commit(); |
| 75 | + |
| 76 | +// Later… |
| 77 | +await room.destroy(); |
| 78 | +``` |
| 79 | + |
| 80 | +Tip: For a working reference, see `packages/loro-websocket/src/e2e.test.ts` which spins up `SimpleServer` and syncs two clients end‑to‑end. |
| 81 | + |
| 82 | +### Optional: SimpleServer hooks |
| 83 | + |
| 84 | +`SimpleServer` accepts optional hooks for basic auth and persistence: |
| 85 | + |
| 86 | +```ts |
| 87 | +const server = new SimpleServer({ |
| 88 | + port: 8787, |
| 89 | + authenticate: async (roomId, crdt, auth) => { |
| 90 | + // return 'read' | 'write' | null to deny |
| 91 | + return "write"; |
| 92 | + }, |
| 93 | + onLoadDocument: async (roomId, crdt) => null, // return snapshot bytes |
| 94 | + onSaveDocument: async (roomId, crdt, data) => { |
| 95 | + // persist snapshot bytes somewhere (e.g., filesystem/db) |
| 96 | + }, |
| 97 | + saveInterval: 60_000, // ms |
| 98 | +}); |
| 99 | +``` |
| 100 | + |
| 101 | +### Alternative: Rust server |
| 102 | + |
| 103 | +The Rust workspace contains a minimal async WebSocket server (`loro-websocket-server`) with optional SQLite persistence. See `rust/loro-websocket-server/examples/simple-server.rs` for a CLI example. |
| 104 | + |
| 105 | +## Protocol Highlights |
| 106 | + |
| 107 | +- Magic bytes per CRDT: "%LOR" (Loro doc), "%EPH" (Loro ephemeral), "%YJS", "%YAW", … |
| 108 | +- Messages: JoinRequest/JoinResponseOk/JoinError, DocUpdate, DocUpdateFragmentHeader/Fragment, UpdateError, Leave |
| 109 | +- Limits: 256 KiB per message; large updates must be fragmented; default reassembly timeout 10s |
| 110 | +- Multi‑room: room ID is part of every message; one connection can join multiple rooms |
| 111 | + |
| 112 | +See `protocol.md` for the full description and error codes. |
| 113 | + |
| 114 | +## Monorepo Dev |
| 115 | + |
| 116 | +- Build all: `pnpm -r build` |
| 117 | +- Test all: `pnpm -r test` |
| 118 | +- Typecheck: `pnpm -r typecheck` |
| 119 | +- Lint: `pnpm -r lint` |
| 120 | + |
| 121 | +Node 18+ is required for local development. |
| 122 | + |
| 123 | +## Licensing |
| 124 | + |
| 125 | +- `loro-protocol`: MIT |
| 126 | +- `loro-websocket`: MIT |
| 127 | +- `loro-adaptors`: MIT |
| 128 | +- Rust workspace crates under `rust/`: AGPL-3.0-only |
| 129 | + |
| 130 | +## Project Structure |
| 131 | + |
| 132 | +``` |
| 133 | +. |
| 134 | +├── protocol.md # Wire protocol spec |
| 135 | +├── packages/ |
| 136 | +│ ├── loro-protocol/ # Core encoders/decoders (MIT) |
| 137 | +│ ├── loro-websocket/ # Client + SimpleServer (MIT) |
| 138 | +│ ├── loro-adaptors/ # Shared CRDT adaptors (MIT) |
| 139 | +├── rust/ # Rust implementations (AGPL) |
| 140 | +│ ├── loro-protocol/ |
| 141 | +│ ├── loro-websocket-client/ |
| 142 | +│ └── loro-websocket-server/ |
| 143 | +└── pnpm-workspace.yaml |
| 144 | +``` |
| 145 | + |
| 146 | +## FAQ |
| 147 | + |
| 148 | +- How do I test locally? Use `SimpleServer` in `loro-websocket` or the Rust server. |
| 149 | +- Can I bring my own auth/storage? Yes — `SimpleServer` and the Rust server provide hooks for auth and persistence. |
0 commit comments