Urgent.News

What's breaking now, across hundreds of outlets.

Tech

Building a Local-First Collaborative Editor With Yjs, Lexical, and IndexedDB

Most collaborative editors break the moment the connection drops. Lattiq does not, because the source of truth is the user's own browser. This is how to wire...

Building a Local-First Collaborative Editor With Yjs, Lexical, and IndexedDB

This article explains how the Lattiq collaborative editor is built using Yjs, Lexical, and IndexedDB. The key ideas are:

1. Collaborative editors often feel brittle because they assume the server is canonical, but Lattiq reverses that assumption by making the client-side document the canonical source.

2. Yjs provides the CRDT (Conflict-free Replicated Data Type) that enables seamless merging of changes from multiple users, even when offline. It uses a position-based data structure so simultaneous edits don't overwrite each other.

3. The editor initialisation sequence is critical: create the Y.Doc, load from IndexedDB, wait for IndexedDB to finish, then render the editor and connect the WebSocket. Rendering too early causes flickering and lost data. Connecting too early leads to a race condition.

4. Lattiq uses a LocalProvider when offline and a WebSocket provider when online. Both providers share the same Y.Doc, so switching between them doesn't fork the document. The provider abstracts the WebSocket connection away from the editor.

5. The editor exposes a sync status state machine to users, indicating whether changes are local-only, syncing, or synced. This gives users visibility into the collaboration state.

In summary, Lattiq leverages Yjs for CRDT-based offline editing, Lexical for the rich text editing component, and IndexedDB for local persistence. A carefully crafted init sequence and provider abstraction enable seamless sync between online and offline modes, delivering a fluid collaborative editing experience.

Rewritten by urgent.news; may contain errors — check the original.

Source: hackernoon.com

Read the original at hackernoon.com →

Report a problem with this story

More in Tech

More from Sunday 22 November →