Kevin Jahns
Yjs Workshop
BOB 2025
=> everything we do nowadays is collaborative
git clone https://github.com/yjs/bob-2025
import * as Y from 'yjs'
const ydoc = new Y.Doc()
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)
})
ymap.set('my key', 'new value') // => { "my key": { action: 'add', oldValue: undefined } }
Exercise:
Bonus: build a counter CRDT
Updates contain a SET of operations
import * as Y from 'yjs'
const ydoc1 = new Y.Doc()
const ymap1 = ydoc1.getMap()
ymap1.set('a', 1)
const ydoc2 = new Y.Doc()
const ymap2 = ydoc2.getMap()
ymap2.set('b', 2)
Y.applyUpdate(ydoc2, Y.encodeStateAsUpdate(ydoc1))
Y.applyUpdate(ydoc1, Y.encodeStateAsUpdate(ydoc2))
console.log(ymap1.toJSON()) // => { a: 1, b: 2 }
console.log(ymap2.toJSON()) // => { a: 1, b: 2 }
import * as Y from 'yjs'
const ydoc1 = new Y.Doc()
const ymap1 = ydoc1.getMap()
ymap1.set('a', 1)
const ydoc2 = new Y.Doc()
const ymap2 = ydoc2.getMap()
ymap2.set('b', 2)
// Sync the differences from ydoc2 into ydoc1
// The state vector describes what ydoc1 contains
const syncStep1 = Y.encodeStateVector(ydoc1)
// Only encode the differences into a update message
const syncStep2 = Y.encodeStateAsUpdate(ydoc2, syncStep1)
// Apply the differences to ydoc1
Y.applyUpdate(ydoc1, syncStep2)
console.log(ymap1.toJSON()) // => { a: 1, b: 2 }
=> Immediately continue with providers
Exercise:
Use inspect.yjs.dev
y-quill setup
+ possibly set up indexeddb
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>
);
}
Editor Bindings
Connectors
Persistence
Cloud Providers
Yjs ported to different programming languages
Control
Image is CC: https://www.flickr.com/photos/nossreh/10987353554
Data
Conflict Resolution
source: https://www.printablee.com/post_50-states-printable-out-maps_185196/
Why can't my favorite file syncing service sync directly to my phone. Instead I first upload to the US, and then sync back
+ Easier to coordinate
- No direct sync
- No sync if service is not reachable
- Harder to scale
Alternatively: Coordination-free RDT
I.e. RDTs that don't require centrality
Data
Conflict Resolution
twitter.com/kevin_jahns