Kevin Jahns
State Synchronization for Collaborative Apps

Realtime & Automatic Conflict Resolution

Replicated Data Types (RDT)
- Abstraction for storing application state
- Optimistic Replication
- Observable
Replicated Data Types (RDT)
- Abstraction for storing application state
- Optimistic Replication
- Observable





RDT Example: Yjs
import * as Y from 'yjs'
const ydoc = new Y.Doc()
const webrtcProvider = new WebrtcProvider('room-name', ydoc, { password: 'optional-room-password' })
// const websocketProvider = new WebsocketProvider(url, 'room-name', ydoc)
// const matrixProvider = new MatrixProvider(ydoc, matrixClient, {type: "alias",alias: "matrix-room-alias"});
const ymap = ydoc.getMap('map')
const yarray = ydoc.getArray('array')
const ytext = ydoc.getText('text')
const yxml = ydoc.getXmlElement('el')
ymap.observe(event => {
console.log(event.changes.keys) // { "my key": { action: 'add', oldValue: undefined } }
})
ymap.set('my key', 'new value')

Lexical Editor by Meta is built on-top of Yjs

Lexical Editor



SyncedStore


SyncedStore
import React from "react";
import { useSyncedStore } from "@syncedstore/react";
import { store } from "./store";
export default function App() {
const state = useSyncedStore(store);
return (
<div>
<p>Todo items:</p>
<ul>
{state.todos.map((todo, i) => (
<li key={i} style={{ textDecoration: todo.completed ? "line-through" : "" }}>
<label>
<input type="checkbox" checked={todo.completed} onClick={() => (todo.completed = !todo.completed)} />
{todo.title}
</label>
</li>
))}
</ul>
<input
onKeyPress={(event) => {
if (event.key === "Enter") {
state.todos.push({ completed: false, title: event.target.value });
target.value = "";
}
}}
/>
</div>
);
}
SyncedStore


RDT: Business appeal
- Quick prototyping
- Realtime & collaborative
- Little setup
- No backend logic
- (Pay as you go)
- (snapshots)
- (offline support)
Yjs is a Conflict-free RDT (CRDT)
Operations are commutative, idempotent, and associative
⇒ o1 • o2 • o3 == o3 • o2 • o1 • o2
Yjs Ecosystem
- y-webrtc
- y-websocket
- hocuspocus
- liveblocks
- y-sweet
- yrs-warp
- matrix-crdt
- ..
- y-prosemirror
- y-quill
- y-codemirror
- y-ace
- y-monaco
- lexical
- ...
Editor Bindings
Connectors
Persistence
- y-indexeddb
- y-leveldb
- ystream
Y CRDT
Yjs ported to different programming languages






Ycs
How I work
- Consulting
- Organizing OSS work
- Sponsors
Control
Control
Image is CC: https://www.flickr.com/photos/nossreh/10987353554
• Who "owns" data
• Who performs
conflict resolution?

Centrality
source: https://www.printablee.com/post_50-states-printable-out-maps_185196/

Centrality


Why can't my favorite file syncing service sync directly to my phone. Instead I first upload to the US, and then sync back
Centrality / Central Place of Control
+ Easier to coordinate
- No direct sync
- No sync if service is not reachable
- Harder to scale
Conflict-free RDTs: CRDTs
Alternatively: Coordination-free RDT
I.e. RDTs that don't require centrality


Shared Control
• Users "own" data
• Everyone performs
conflict resolution
Sync Peer-to-Peer



Sync on the Edge





Make it Available Offline







y-redis
- Infinitely scalable backend for y-websocket
- Distributes updates efficiently using redis streams
- Stores documents in S3 or Postgres
- Only syncs a document at a time
Ystream
- A database for CRDTs, based on LMDB
- Sync millions of documents in seconds
- Revision control for humans
- Users own their data
Current focus

twitter.com/kevin_jahns

State Synchronization - reaktor presentation
By Kevin Jahns
State Synchronization - reaktor presentation
- 122