GithubHelp home page GithubHelp logo

yjs / yjs Goto Github PK

View Code? Open in Web Editor NEW
15.2K 118.0 554.0 70.8 MB

Shared data types for building collaborative software

Home Page: https://docs.yjs.dev

License: Other

JavaScript 99.97% HTML 0.03%
yjs crdt shared-editing collaboration collaborative-editing peer-to-peer p2p offline-first decentralized realtime

yjs's Introduction

Yjs

A CRDT framework with a powerful abstraction of shared data

Yjs is a CRDT implementation that exposes its internal data structure as shared types. Shared types are common data types like Map or Array with superpowers: changes are automatically distributed to other peers and merged without merge conflicts.

Yjs is network agnostic (p2p!), supports many existing rich text editors, offline editing, version snapshots, undo/redo and shared cursors. It scales well with an unlimited number of users and is well suited for even large documents.

👷‍♀️ If you are looking for professional support, please consider supporting this project via a "support contract" on GitHub Sponsors. I will attend your issues quicker and we can discuss questions and problems in regular video conferences. Otherwise you can find help on our community discussion board.

Sponsorship

Please contribute to the project financially - especially if your company relies on Yjs. Become a Sponsor

Professional Support

  • Support Contract with the Maintainer - By contributing financially to the open-source Yjs project, you can receive professional support directly from the author. This includes the opportunity for weekly video calls to discuss your specific challenges.
  • Synergy Codes - Specializing in consulting and developing real-time collaborative editing solutions for visual apps, Synergy Codes focuses on interactive diagrams, complex graphs, charts, and various data visualization types. Their expertise empowers developers to build engaging and interactive visual experiences leveraging the power of Yjs. See their work in action at Visual Collaboration Showcase.

Who is using Yjs

  • AFFiNE A local-first, privacy-first, open source knowledge base. 🌟
  • Huly - Open Source All-in-One Project Management Platform :star2:
  • Cargo Site builder for designers and artists 🌟
  • Gitbook Knowledge management for technical teams 🌟
  • Evernote Note-taking app 🌟
  • Lessonspace Enterprise platform for virtual classrooms and online training 🌟
  • Dynaboard Build web apps collaboratively. ⭐
  • Relm A collaborative gameworld for teamwork and community. ⭐
  • Room.sh A meeting application with integrated collaborative drawing, editing, and coding tools. ⭐
  • Nimbus Note A note-taking app designed by Nimbus Web. ⭐
  • Pluxbox RadioManager A web-based app to collaboratively organize radio broadcasts. ⭐
  • modyfi - Modyfi is the design platform built for multidisciplinary designers. Design, generate, animate, and more — without switching between apps. ⭐
  • Sana A learning platform with collaborative text editing powered by Yjs.
  • Serenity Notes End-to-end encrypted collaborative notes app.
  • PRSM Collaborative mind-mapping and system visualisation. (source)
  • Alldone A next-gen project management and collaboration platform.
  • Living Spec A modern way for product teams to collaborate.
  • Slidebeamer Presentation app.
  • BlockSurvey End-to-end encryption for your forms/surveys.
  • Skiff Private, decentralized workspace.
  • JupyterLab Collaborative computational Notebooks
  • JupyterCad Extension to JupyterLab that enables collaborative editing of 3d FreeCAD Models.
  • Hyperquery A collaborative data workspace for sharing analyses, documentation, spreadsheets, and dashboards.
  • Nosgestesclimat The french carbon footprint calculator has a group P2P mode based on yjs
  • oorja.io Online meeting spaces extensible with collaborative apps, end-to-end encrypted.
  • LegendKeeper Collaborative campaign planner and worldbuilding app for tabletop RPGs.
  • IllumiDesk Build courses and content with A.I.
  • btw Open-source Medium alternative
  • AWS SageMaker Tools for building Machine Learning Models
  • linear Streamline issues, projects, and product roadmaps.
  • btw - Personal website builder
  • AWS SageMaker - Machine Learning Service
  • Arkiter - Live interview software
  • Appflowy - They use Yrs
  • Multi.app - Multiplayer app sharing: Point, draw and edit in shared apps as if they're on your computer. They are using Yrs.
  • AppMaster A No-Code platform for creating production-ready applications with source code generation.
  • Synthesia - Collaborative Video Editor
  • thinkdeli - A fast and simple notes app powered by AI
  • ourboard - A collaborative whiteboard applicaiton

Table of Contents

Overview

This repository contains a collection of shared types that can be observed for changes and manipulated concurrently. Network functionality and two-way-bindings are implemented in separate modules.

Bindings

Name Cursors Binding Demo
ProseMirror                                                   y-prosemirror demo
Quill y-quill demo
CodeMirror y-codemirror demo
Monaco y-monaco demo
Slate slate-yjs demo
BlockSuite (native) demo
valtio valtio-yjs demo
immer immer-yjs demo
React / Vue / Svelte / MobX SyncedStore demo
mobx-keystone mobx-keystone-yjs demo

Providers

Setting up the communication between clients, managing awareness information, and storing shared data for offline usage is quite a hassle. Providers manage all that for you and are the perfect starting point for your collaborative app.

This list of providers is incomplete. Please open PRs to add your providers to this list!

Connection Providers

y-websocket
A module that contains a simple websocket backend and a websocket client that connects to that backend. y-redis, y-sweet, and ypy-websocket (see below) are alternative backends to y-websocket.
y-webrtc
Propagates document updates peer-to-peer using WebRTC. The peers exchange signaling data over signaling servers. Publically available signaling servers are available. Communication over the signaling servers can be encrypted by providing a shared secret, keeping the connection information and the shared document private.
@liveblocks/yjs
Liveblocks Yjs provides a fully hosted WebSocket infrastructure and persisted data store for Yjs documents. No configuration or maintenance is required. It also features Yjs webhook events, REST API to read and update Yjs documents, and a browser DevTools extension.
y-sweet
A standalone yjs server with persistence to S3 or filesystem. They offer a cloud service as well.
PartyKit
Cloud service for building multiplayer apps.
y-libp2p
Uses libp2p to propagate updates via GossipSub. Also includes a peer-sync mechanism to catch up on missed updates.
y-dat
[WIP] Write document updates efficiently to the dat network using multifeed. Each client has an append-only log of CRDT local updates (hypercore). Multifeed manages and sync hypercores and y-dat listens to changes and applies them to the Yjs document.
Matrix-CRDT
Use Matrix as an off-the-shelf backend for Yjs by using the MatrixProvider. Use Matrix as transport and storage of Yjs updates, so you can focus building your client app and Matrix can provide powerful features like Authentication, Authorization, Federation, hosting (self-hosting or SaaS) and even End-to-End Encryption (E2EE).
yrb-actioncable
An ActionCable companion for Yjs clients. There is a fitting redis extension as well.
ypy-websocket
Websocket backend, written in Python.
Tinybase
The reactive data store for local-first apps. They support multiple CRDTs and different network technologies.
y-webxdc
Provider for sharing data in webxdc chat apps.

Persistence Providers

y-indexeddb
Efficiently persists document updates to the browsers indexeddb database. The document is immediately available and only diffs need to be synced through the network provider.
y-mongodb-provider
Adds persistent storage to a server with MongoDB. Can be used with the y-websocket provider.
@toeverything/y-indexeddb
Like y-indexeddb, but with sub-documents support and fully TypeScript.
y-fire
A database and connection provider for Yjs based on Firestore.

Ports

There are several Yjs-compatible ports to other programming languages.

  • y-octo - Rust implementation by AFFiNE
  • y-crdt - Rust implementation with multiple language bindings to other languages
  • ycs - .Net compatible C# implementation.

Getting Started

Install Yjs and a provider with your favorite package manager:

npm i yjs y-websocket

Start the y-websocket server:

PORT=1234 node ./node_modules/y-websocket/bin/server.js

Example: Observe types

import * as Y from 'yjs';

const doc = new Y.Doc();
const yarray = doc.getArray('my-array')
yarray.observe(event => {
  console.log('yarray was modified')
})
// every time a local or remote client modifies yarray, the observer is called
yarray.insert(0, ['val']) // => "yarray was modified"

Example: Nest types

Remember, shared types are just plain old data types. The only limitation is that a shared type must exist only once in the shared document.

const ymap = doc.getMap('map')
const foodArray = new Y.Array()
foodArray.insert(0, ['apple', 'banana'])
ymap.set('food', foodArray)
ymap.get('food') === foodArray // => true
ymap.set('fruit', foodArray) // => Error! foodArray is already defined

Now you understand how types are defined on a shared document. Next you can jump to the demo repository or continue reading the API docs.

Example: Using and combining providers

Any of the Yjs providers can be combined with each other. So you can sync data over different network technologies.

In most cases you want to use a network provider (like y-websocket or y-webrtc) in combination with a persistence provider (y-indexeddb in the browser). Persistence allows you to load the document faster and to persist data that is created while offline.

For the sake of this demo we combine two different network providers with a persistence provider.

import * as Y from 'yjs'
import { WebrtcProvider } from 'y-webrtc'
import { WebsocketProvider } from 'y-websocket'
import { IndexeddbPersistence } from 'y-indexeddb'

const ydoc = new Y.Doc()

// this allows you to instantly get the (cached) documents data
const indexeddbProvider = new IndexeddbPersistence('count-demo', ydoc)
indexeddbProvider.whenSynced.then(() => {
  console.log('loaded data from indexed db')
})

// Sync clients with the y-webrtc provider.
const webrtcProvider = new WebrtcProvider('count-demo', ydoc)

// Sync clients with the y-websocket provider
const websocketProvider = new WebsocketProvider(
  'wss://demos.yjs.dev', 'count-demo', ydoc
)

// array of numbers which produce a sum
const yarray = ydoc.getArray('count')

// observe changes of the sum
yarray.observe(event => {
  // print updates when the data changes
  console.log('new sum: ' + yarray.toArray().reduce((a,b) => a + b))
})

// add 1 to the sum
yarray.push([1]) // => "new sum: 1"

API

import * as Y from 'yjs'

Shared Types

Y.Array

A shareable Array-like type that supports efficient insert/delete of elements at any position. Internally it uses a linked list of Arrays that is split when necessary.

const yarray = new Y.Array()
parent:Y.AbstractType|null
insert(index:number, content:Array<object|boolean|Array|string|number|null|Uint8Array|Y.Type>)
Insert content at index. Note that content is an array of elements. I.e. array.insert(0, [1]) splices the list and inserts 1 at position 0.
push(Array<Object|boolean|Array|string|number|null|Uint8Array|Y.Type>)
unshift(Array<Object|boolean|Array|string|number|null|Uint8Array|Y.Type>)
delete(index:number, length:number)
get(index:number)
slice(start:number, end:number):Array<Object|boolean|Array|string|number|null|Uint8Array|Y.Type>
Retrieve a range of content
length:number
forEach(function(value:object|boolean|Array|string|number|null|Uint8Array|Y.Type, index:number, array: Y.Array))
map(function(T, number, YArray):M):Array<M>
toArray():Array<object|boolean|Array|string|number|null|Uint8Array|Y.Type>
Copies the content of this YArray to a new Array.
toJSON():Array<Object|boolean|Array|string|number|null>
Copies the content of this YArray to a new Array. It transforms all child types to JSON using their toJSON method.
[Symbol.Iterator]
Returns an YArray Iterator that contains the values for each index in the array.
for (let value of yarray) { .. }
observe(function(YArrayEvent, Transaction):void)
Adds an event listener to this type that will be called synchronously every time this type is modified. In the case this type is modified in the event listener, the event listener will be called again after the current event listener returns.
unobserve(function(YArrayEvent, Transaction):void)
Removes an observe event listener from this type.
observeDeep(function(Array<YEvent>, Transaction):void)
Adds an event listener to this type that will be called synchronously every time this type or any of its children is modified. In the case this type is modified in the event listener, the event listener will be called again after the current event listener returns. The event listener receives all Events created by itself or any of its children.
unobserveDeep(function(Array<YEvent>, Transaction):void)
Removes an observeDeep event listener from this type.
Y.Map

A shareable Map type.

const ymap = new Y.Map()
parent:Y.AbstractType|null
size: number
Total number of key/value pairs.
get(key:string):object|boolean|string|number|null|Uint8Array|Y.Type
set(key:string, value:object|boolean|string|number|null|Uint8Array|Y.Type)
delete(key:string)
has(key:string):boolean
get(index:number)
clear()
Removes all elements from this YMap.
clone():Y.Map
Clone this type into a fresh Yjs type.
toJSON():Object<string, Object|boolean|Array|string|number|null|Uint8Array>
Copies the [key,value] pairs of this YMap to a new Object.It transforms all child types to JSON using their toJSON method.
forEach(function(value:object|boolean|Array|string|number|null|Uint8Array|Y.Type, key:string, map: Y.Map))
Execute the provided function once for every key-value pair.
[Symbol.Iterator]
Returns an Iterator of [key, value] pairs.
for (let [key, value] of ymap) { .. }
entries()
Returns an Iterator of [key, value] pairs.
values()
Returns an Iterator of all values.
keys()
Returns an Iterator of all keys.
observe(function(YMapEvent, Transaction):void)
Adds an event listener to this type that will be called synchronously every time this type is modified. In the case this type is modified in the event listener, the event listener will be called again after the current event listener returns.
unobserve(function(YMapEvent, Transaction):void)
Removes an observe event listener from this type.
observeDeep(function(Array<YEvent>, Transaction):void)
Adds an event listener to this type that will be called synchronously every time this type or any of its children is modified. In the case this type is modified in the event listener, the event listener will be called again after the current event listener returns. The event listener receives all Events created by itself or any of its children.
unobserveDeep(function(Array<YEvent>, Transaction):void)
Removes an observeDeep event listener from this type.
Y.Text

A shareable type that is optimized for shared editing on text. It allows to assign properties to ranges in the text. This makes it possible to implement rich-text bindings to this type.

This type can also be transformed to the delta format. Similarly the YTextEvents compute changes as deltas.

const ytext = new Y.Text()
parent:Y.AbstractType|null
insert(index:number, content:string, [formattingAttributes:Object<string,string>])
Insert a string at index and assign formatting attributes to it.
ytext.insert(0, 'bold text', { bold: true })
delete(index:number, length:number)
format(index:number, length:number, formattingAttributes:Object<string,string>)
Assign formatting attributes to a range in the text
applyDelta(delta: Delta, opts:Object<string,any>)
See Quill Delta Can set options for preventing remove ending newLines, default is true.
ytext.applyDelta(delta, { sanitize: false })
length:number
toString():string
Transforms this type, without formatting options, into a string.
toJSON():string
See toString
toDelta():Delta
Transforms this type to a Quill Delta
observe(function(YTextEvent, Transaction):void)
Adds an event listener to this type that will be called synchronously every time this type is modified. In the case this type is modified in the event listener, the event listener will be called again after the current event listener returns.
unobserve(function(YTextEvent, Transaction):void)
Removes an observe event listener from this type.
observeDeep(function(Array<YEvent>, Transaction):void)
Adds an event listener to this type that will be called synchronously every time this type or any of its children is modified. In the case this type is modified in the event listener, the event listener will be called again after the current event listener returns. The event listener receives all Events created by itself or any of its children.
unobserveDeep(function(Array<YEvent>, Transaction):void)
Removes an observeDeep event listener from this type.
Y.XmlFragment

A container that holds an Array of Y.XmlElements.

const yxml = new Y.XmlFragment()
parent:Y.AbstractType|null
firstChild:Y.XmlElement|Y.XmlText|null
insert(index:number, content:Array<Y.XmlElement|Y.XmlText>)
delete(index:number, length:number)
get(index:number)
slice(start:number, end:number):Array<Y.XmlElement|Y.XmlText>
Retrieve a range of content
length:number
clone():Y.XmlFragment
Clone this type into a fresh Yjs type.
toArray():Array<Y.XmlElement|Y.XmlText>
Copies the children to a new Array.
toDOM():DocumentFragment
Transforms this type and all children to new DOM elements.
toString():string
Get the XML serialization of all descendants.
toJSON():string
See toString.
createTreeWalker(filter: function(AbstractType<any>):boolean):Iterable
Create an Iterable that walks through the children.
observe(function(YXmlEvent, Transaction):void)
Adds an event listener to this type that will be called synchronously every time this type is modified. In the case this type is modified in the event listener, the event listener will be called again after the current event listener returns.
unobserve(function(YXmlEvent, Transaction):void)
Removes an observe event listener from this type.
observeDeep(function(Array<YEvent>, Transaction):void)
Adds an event listener to this type that will be called synchronously every time this type or any of its children is modified. In the case this type is modified in the event listener, the event listener will be called again after the current event listener returns. The event listener receives all Events created by itself or any of its children.
unobserveDeep(function(Array<YEvent>, Transaction):void)
Removes an observeDeep event listener from this type.
Y.XmlElement

A shareable type that represents an XML Element. It has a nodeName, attributes, and a list of children. But it makes no effort to validate its content and be actually XML compliant.

const yxml = new Y.XmlElement()
parent:Y.AbstractType|null
firstChild:Y.XmlElement|Y.XmlText|null
nextSibling:Y.XmlElement|Y.XmlText|null
prevSibling:Y.XmlElement|Y.XmlText|null
insert(index:number, content:Array<Y.XmlElement|Y.XmlText>)
delete(index:number, length:number)
get(index:number)
length:number
setAttribute(attributeName:string, attributeValue:string)
removeAttribute(attributeName:string)
getAttribute(attributeName:string):string
getAttributes():Object<string,string>
get(i:number):Y.XmlElement|Y.XmlText
Retrieve the i-th element.
slice(start:number, end:number):Array<Y.XmlElement|Y.XmlText>
Retrieve a range of content
clone():Y.XmlElement
Clone this type into a fresh Yjs type.
toArray():Array<Y.XmlElement|Y.XmlText>
Copies the children to a new Array.
toDOM():Element
Transforms this type and all children to a new DOM element.
toString():string
Get the XML serialization of all descendants.
toJSON():string
See toString.
observe(function(YXmlEvent, Transaction):void)
Adds an event listener to this type that will be called synchronously every time this type is modified. In the case this type is modified in the event listener, the event listener will be called again after the current event listener returns.
unobserve(function(YXmlEvent, Transaction):void)
Removes an observe event listener from this type.
observeDeep(function(Array<YEvent>, Transaction):void)
Adds an event listener to this type that will be called synchronously every time this type or any of its children is modified. In the case this type is modified in the event listener, the event listener will be called again after the current event listener returns. The event listener receives all Events created by itself or any of its children.
unobserveDeep(function(Array<YEvent>, Transaction):void)
Removes an observeDeep event listener from this type.

Y.Doc

const doc = new Y.Doc()
clientID
A unique id that identifies this client. (readonly)
gc
Whether garbage collection is enabled on this doc instance. Set `doc.gc = false` in order to disable gc and be able to restore old content. See https://github.com/yjs/yjs#yjs-crdt-algorithm for more information about gc in Yjs.
transact(function(Transaction):void [, origin:any])
Every change on the shared document happens in a transaction. Observer calls and the update event are called after each transaction. You should bundle changes into a single transaction to reduce the amount of event calls. I.e. doc.transact(() => { yarray.insert(..); ymap.set(..) }) triggers a single change event.
You can specify an optional origin parameter that is stored on transaction.origin and on('update', (update, origin) => ..).
toJSON():any
Deprecated: It is recommended to call toJSON directly on the shared types. Converts the entire document into a js object, recursively traversing each yjs type. Doesn't log types that have not been defined (using ydoc.getType(..)).
get(string, Y.[TypeClass]):[Type]
Define a shared type.
getArray(string):Y.Array
Define a shared Y.Array type. Is equivalent to y.get(string, Y.Array).
getMap(string):Y.Map
Define a shared Y.Map type. Is equivalent to y.get(string, Y.Map).
getText(string):Y.Text
Define a shared Y.Text type. Is equivalent to y.get(string, Y.Text).
getXmlElement(string, string):Y.XmlElement
Define a shared Y.XmlElement type. Is equivalent to y.get(string, Y.XmlElement).
getXmlFragment(string):Y.XmlFragment
Define a shared Y.XmlFragment type. Is equivalent to y.get(string, Y.XmlFragment).
on(string, function)
Register an event listener on the shared type
off(string, function)
Unregister an event listener from the shared type

Y.Doc Events

on('update', function(updateMessage:Uint8Array, origin:any, Y.Doc):void)
Listen to document updates. Document updates must be transmitted to all other peers. You can apply document updates in any order and multiple times. Use `updateV2` to receive V2 events.
on('beforeTransaction', function(Y.Transaction, Y.Doc):void)
Emitted before each transaction.
on('afterTransaction', function(Y.Transaction, Y.Doc):void)
Emitted after each transaction.
on('beforeAllTransactions', function(Y.Doc):void)
Transactions can be nested (e.g. when an event within a transaction calls another transaction). Emitted before the first transaction.
on('afterAllTransactions', function(Y.Doc, Array<Y.Transaction>):void)
Emitted after the last transaction is cleaned up.

Document Updates

Changes on the shared document are encoded into document updates. Document updates are commutative and idempotent. This means that they can be applied in any order and multiple times.

Example: Listen to update events and apply them on remote client

const doc1 = new Y.Doc()
const doc2 = new Y.Doc()

doc1.on('update', update => {
  Y.applyUpdate(doc2, update)
})

doc2.on('update', update => {
  Y.applyUpdate(doc1, update)
})

// All changes are also applied to the other document
doc1.getArray('myarray').insert(0, ['Hello doc2, you got this?'])
doc2.getArray('myarray').get(0) // => 'Hello doc2, you got this?'

Yjs internally maintains a state vector that denotes the next expected clock from each client. In a different interpretation it holds the number of structs created by each client. When two clients sync, you can either exchange the complete document structure or only the differences by sending the state vector to compute the differences.

Example: Sync two clients by exchanging the complete document structure

const state1 = Y.encodeStateAsUpdate(ydoc1)
const state2 = Y.encodeStateAsUpdate(ydoc2)
Y.applyUpdate(ydoc1, state2)
Y.applyUpdate(ydoc2, state1)

Example: Sync two clients by computing the differences

This example shows how to sync two clients with the minimal amount of exchanged data by computing only the differences using the state vector of the remote client. Syncing clients using the state vector requires another roundtrip, but can save a lot of bandwidth.

const stateVector1 = Y.encodeStateVector(ydoc1)
const stateVector2 = Y.encodeStateVector(ydoc2)
const diff1 = Y.encodeStateAsUpdate(ydoc1, stateVector2)
const diff2 = Y.encodeStateAsUpdate(ydoc2, stateVector1)
Y.applyUpdate(ydoc1, diff2)
Y.applyUpdate(ydoc2, diff1)

Example: Syncing clients without loading the Y.Doc

It is possible to sync clients and compute delta updates without loading the Yjs document to memory. Yjs exposes an API to compute the differences directly on the binary document updates.

// encode the current state as a binary buffer
let currentState1 = Y.encodeStateAsUpdate(ydoc1)
let currentState2 = Y.encodeStateAsUpdate(ydoc2)
// now we can continue syncing clients using state vectors without using the Y.Doc
ydoc1.destroy()
ydoc2.destroy()

const stateVector1 = Y.encodeStateVectorFromUpdate(currentState1)
const stateVector2 = Y.encodeStateVectorFromUpdate(currentState2)
const diff1 = Y.diffUpdate(currentState1, stateVector2)
const diff2 = Y.diffUpdate(currentState2, stateVector1)

// sync clients
currentState1 = Y.mergeUpdates([currentState1, diff2])
currentState2 = Y.mergeUpdates([currentState2, diff1])

Obfuscating Updates

If one of your users runs into a weird bug (e.g. the rich-text editor throws error messages), then you don't have to request the full document from your user. Instead, they can obfuscate the document (i.e. replace the content with meaningless generated content) before sending it to you. Note that someone might still deduce the type of content by looking at the general structure of the document. But this is much better than requesting the original document.

Obfuscated updates contain all the CRDT-related data that is required for merging. So it is safe to merge obfuscated updates.

const ydoc = new Y.Doc()
// perform some changes..
ydoc.getText().insert(0, 'hello world')
const update = Y.encodeStateAsUpdate(ydoc)
// the below update contains scrambled data
const obfuscatedUpdate = Y.obfuscateUpdate(update)
const ydoc2 = new Y.Doc()
Y.applyUpdate(ydoc2, obfuscatedUpdate)
ydoc2.getText().toString() // => "00000000000"

Using V2 update format

Yjs implements two update formats. By default you are using the V1 update format. You can opt-in into the V2 update format which provides much better compression. It is not yet used by all providers. However, you can already use it if you are building your own provider. All below functions are available with the suffix "V2". E.g. Y.applyUpdateY.applyUpdateV2. Also when listening to updates you need to specifically need listen for V2 events e.g. yDoc.on('updateV2', …). We also support conversion functions between both formats: Y.convertUpdateFormatV1ToV2 & Y.convertUpdateFormatV2ToV1.

Update API

Y.applyUpdate(Y.Doc, update:Uint8Array, [transactionOrigin:any])
Apply a document update on the shared document. Optionally you can specify transactionOrigin that will be stored on transaction.origin and ydoc.on('update', (update, origin) => ..).
Y.encodeStateAsUpdate(Y.Doc, [encodedTargetStateVector:Uint8Array]):Uint8Array
Encode the document state as a single update message that can be applied on the remote document. Optionally specify the target state vector to only write the differences to the update message.
Y.encodeStateVector(Y.Doc):Uint8Array
Computes the state vector and encodes it into an Uint8Array.
Y.mergeUpdates(Array<Uint8Array>)
Merge several document updates into a single document update while removing duplicate information. The merged document update is always smaller than the separate updates because of the compressed encoding.
Y.encodeStateVectorFromUpdate(Uint8Array): Uint8Array
Computes the state vector from a document update and encodes it into an Uint8Array.
Y.diffUpdate(update: Uint8Array, stateVector: Uint8Array): Uint8Array
Encode the missing differences to another update message. This function works similarly to Y.encodeStateAsUpdate(ydoc, stateVector) but works on updates instead.
convertUpdateFormatV1ToV2
Convert V1 update format to the V2 update format.
convertUpdateFormatV2ToV1
Convert V2 update format to the V1 update format.

Relative Positions

When working with collaborative documents, we often need to work with positions. Positions may represent cursor locations, selection ranges, or even assign a comment to a range of text. Normal index-positions (expressed as integers) are not convenient to use because the index-range is invalidated as soon as a remote change manipulates the document. Relative positions give you a powerful API to express positions.

A relative position is fixated to an element in the shared document and is not affected by remote changes. I.e. given the document "a|c", the relative position is attached to c. When a remote user modifies the document by inserting a character before the cursor, the cursor will stay attached to the character c. insert(1, 'x')("a|c") = "ax|c". When the relative position is set to the end of the document, it will stay attached to the end of the document.

Example: Transform to RelativePosition and back

const relPos = Y.createRelativePositionFromTypeIndex(ytext, 2)
const pos = Y.createAbsolutePositionFromRelativePosition(relPos, doc)
pos.type === ytext // => true
pos.index === 2 // => true

Example: Send relative position to remote client (json)

const relPos = Y.createRelativePositionFromTypeIndex(ytext, 2)
const encodedRelPos = JSON.stringify(relPos)
// send encodedRelPos to remote client..
const parsedRelPos = JSON.parse(encodedRelPos)
const pos = Y.createAbsolutePositionFromRelativePosition(parsedRelPos, remoteDoc)
pos.type === remoteytext // => true
pos.index === 2 // => true

Example: Send relative position to remote client (Uint8Array)

const relPos = Y.createRelativePositionFromTypeIndex(ytext, 2)
const encodedRelPos = Y.encodeRelativePosition(relPos)
// send encodedRelPos to remote client..
const parsedRelPos = Y.decodeRelativePosition(encodedRelPos)
const pos = Y.createAbsolutePositionFromRelativePosition(parsedRelPos, remoteDoc)
pos.type === remoteytext // => true
pos.index === 2 // => true
Y.createRelativePositionFromTypeIndex(type:Uint8Array|Y.Type, index: number [, assoc=0])
Create a relative position fixated to the i-th element in any sequence-like shared type (if assoc >= 0). By default, the position associates with the character that comes after the specified index position. If assoc < 0, then the relative position associates with the character before the specified index position.
Y.createAbsolutePositionFromRelativePosition(RelativePosition, Y.Doc): { type: Y.AbstractType, index: number, assoc: number } | null
Create an absolute position from a relative position. If the relative position cannot be referenced, or the type is deleted, then the result is null.
Y.encodeRelativePosition(RelativePosition):Uint8Array
Encode a relative position to an Uint8Array. Binary data is the preferred encoding format for document updates. If you prefer JSON encoding, you can simply JSON.stringify / JSON.parse the relative position instead.
Y.decodeRelativePosition(Uint8Array):RelativePosition
Decode a binary-encoded relative position to a RelativePositon object.

Y.UndoManager

Yjs ships with an Undo/Redo manager for selective undo/redo of changes on a Yjs type. The changes can be optionally scoped to transaction origins.

const ytext = doc.getText('text')
const undoManager = new Y.UndoManager(ytext)

ytext.insert(0, 'abc')
undoManager.undo()
ytext.toString() // => ''
undoManager.redo()
ytext.toString() // => 'abc'
constructor(scope:Y.AbstractType|Array<Y.AbstractType> [, {captureTimeout:number,trackedOrigins:Set<any>,deleteFilter:function(item):boolean}])
Accepts either single type as scope or an array of types.
undo()
redo()
stopCapturing()
on('stack-item-added', { stackItem: { meta: Map<any,any> }, type: 'undo' | 'redo' })
Register an event that is called when a StackItem is added to the undo- or the redo-stack.
on('stack-item-updated', { stackItem: { meta: Map<any,any> }, type: 'undo' | 'redo' })
Register an event that is called when an existing StackItem is updated. This happens when two changes happen within a "captureInterval".
on('stack-item-popped', { stackItem: { meta: Map<any,any> }, type: 'undo' | 'redo' })
Register an event that is called when a StackItem is popped from the undo- or the redo-stack.
on('stack-cleared', { undoStackCleared: boolean, redoStackCleared: boolean })
Register an event that is called when the undo- and/or the redo-stack is cleared.

Example: Stop Capturing

UndoManager merges Undo-StackItems if they are created within time-gap smaller than options.captureTimeout. Call um.stopCapturing() so that the next StackItem won't be merged.

// without stopCapturing
ytext.insert(0, 'a')
ytext.insert(1, 'b')
undoManager.undo()
ytext.toString() // => '' (note that 'ab' was removed)
// with stopCapturing
ytext.insert(0, 'a')
undoManager.stopCapturing()
ytext.insert(0, 'b')
undoManager.undo()
ytext.toString() // => 'a' (note that only 'b' was removed)

Example: Specify tracked origins

Every change on the shared document has an origin. If no origin was specified, it defaults to null. By specifying trackedOrigins you can selectively specify which changes should be tracked by UndoManager. The UndoManager instance is always added to trackedOrigins.

class CustomBinding {}

const ytext = doc.getText('text')
const undoManager = new Y.UndoManager(ytext, {
  trackedOrigins: new Set([42, CustomBinding])
})

ytext.insert(0, 'abc')
undoManager.undo()
ytext.toString() // => 'abc' (does not track because origin `null` and not part
                 //           of `trackedTransactionOrigins`)
ytext.delete(0, 3) // revert change

doc.transact(() => {
  ytext.insert(0, 'abc')
}, 42)
undoManager.undo()
ytext.toString() // => '' (tracked because origin is an instance of `trackedTransactionorigins`)

doc.transact(() => {
  ytext.insert(0, 'abc')
}, 41)
undoManager.undo()
ytext.toString() // => 'abc' (not tracked because 41 is not an instance of
                 //        `trackedTransactionorigins`)
ytext.delete(0, 3) // revert change

doc.transact(() => {
  ytext.insert(0, 'abc')
}, new CustomBinding())
undoManager.undo()
ytext.toString() // => '' (tracked because origin is a `CustomBinding` and
                 //        `CustomBinding` is in `trackedTransactionorigins`)

Example: Add additional information to the StackItems

When undoing or redoing a previous action, it is often expected to restore additional meta information like the cursor location or the view on the document. You can assign meta-information to Undo-/Redo-StackItems.

const ytext = doc.getText('text')
const undoManager = new Y.UndoManager(ytext, {
  trackedOrigins: new Set([42, CustomBinding])
})

undoManager.on('stack-item-added', event => {
  // save the current cursor location on the stack-item
  event.stackItem.meta.set('cursor-location', getRelativeCursorLocation())
})

undoManager.on('stack-item-popped', event => {
  // restore the current cursor location on the stack-item
  restoreCursorLocation(event.stackItem.meta.get('cursor-location'))
})

Yjs CRDT Algorithm

Conflict-free replicated data types (CRDT) for collaborative editing are an alternative approach to operational transformation (OT). A very simple differentiation between the two approaches is that OT attempts to transform index positions to ensure convergence (all clients end up with the same content), while CRDTs use mathematical models that usually do not involve index transformations, like linked lists. OT is currently the de-facto standard for shared editing on text. OT approaches that support shared editing without a central source of truth (a central server) require too much bookkeeping to be viable in practice. CRDTs are better suited for distributed systems, provide additional guarantees that the document can be synced with remote clients, and do not require a central source of truth.

Yjs implements a modified version of the algorithm described in this paper. This article explains a simple optimization on the CRDT model and gives more insight about the performance characteristics in Yjs. More information about the specific implementation is available in INTERNALS.md and in this walkthrough of the Yjs codebase.

CRDTs that are suitable for shared text editing suffer from the fact that they only grow in size. There are CRDTs that do not grow in size, but they do not have the characteristics that are benificial for shared text editing (like intention preservation). Yjs implements many improvements to the original algorithm that diminish the trade-off that the document only grows in size. We can't garbage collect deleted structs (tombstones) while ensuring a unique order of the structs. But we can 1. merge preceeding structs into a single struct to reduce the amount of meta information, 2. we can delete content from the struct if it is deleted, and 3. we can garbage collect tombstones if we don't care about the order of the structs anymore (e.g. if the parent was deleted).

Examples:

  1. If a user inserts elements in sequence, the struct will be merged into a single struct. E.g. text.insert(0, 'a'), text.insert(1, 'b'); is first represented as two structs ([{id: {client, clock: 0}, content: 'a'}, {id: {client, clock: 1}, content: 'b'}) and then merged into a single struct: [{id: {client, clock: 0}, content: 'ab'}].
  2. When a struct that contains content (e.g. ItemString) is deleted, the struct will be replaced with an ItemDeleted that does not contain content anymore.
  3. When a type is deleted, all child elements are transformed to GC structs. A GC struct only denotes the existence of a struct and that it is deleted. GC structs can always be merged with other GC structs if the id's are adjacent.

Especially when working on structured content (e.g. shared editing on ProseMirror), these improvements yield very good results when benchmarking random document edits. In practice they show even better results, because users usually edit text in sequence, resulting in structs that can easily be merged. The benchmarks show that even in the worst case scenario that a user edits text from right to left, Yjs achieves good performance even for huge documents.

State Vector

Yjs has the ability to exchange only the differences when syncing two clients. We use lamport timestamps to identify structs and to track in which order a client created them. Each struct has an struct.id = { client: number, clock: number} that uniquely identifies a struct. We define the next expected clock by each client as the state vector. This data structure is similar to the version vectors data structure. But we use state vectors only to describe the state of the local document, so we can compute the missing struct of the remote client. We do not use it to track causality.

License and Author

Yjs and all related projects are MIT licensed.

Yjs is based on my research as a student at the RWTH i5. Now I am working on Yjs in my spare time.

Fund this project by donating on GitHub Sponsors or hiring me as a contractor for your collaborative app.

yjs's People

Contributors

adamchel-synth avatar boschdev avatar canadaduane avatar dhenneke avatar dkuhnert avatar dmonad avatar doodlewind avatar himself65 avatar istvank avatar ja2nicholl avatar kisama avatar mansehej avatar mohe2015 avatar mtn avatar mylesj avatar neftaly avatar nickmitin avatar nikgraf avatar nilset avatar patrickshaw avatar raedle avatar raineorshine avatar rileyjshaw avatar samduvall avatar samypesse avatar satyajeetjadhav avatar synix avatar ukarlsson avatar viktorqvarfordt avatar yousefed avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

yjs's Issues

SyncMeta model updates to certain elements sometimes stop being applied in Yjs

This issue is related to both Yjs and SyncMeta. I'm using the default Yjs (fallback) server. When loading a model into a SyncMeta instance via the Import Tool (former Debug Widget), it might happen, that certain elements in it "break", by the means, that the changes you apply to them will be present in the local Yjs representation, but won't be propagated to other clients. Those excact elements will stay broken, even if you refresh the page or delete and re-import the model.

The error is hard to reproduce, as it is very rare, and hard to notice once it happens. Atm, I can't think of another way then importing a model over and over again, then moving around random nodes, in oder to see if they change on another client too.

errormodel.zip

Huly®: YJS-36

Destroy a Yjs instance

The .destroy() method is not correctly implemented. It should destroy the database, and cleanup all bindings.

Therefore, the types also must implement a destroy() method.

Huly®: YJS-24

Server Implementation Example 404

Hi there,

Yatta looks awesome. I started to implement it today after spending 6 1/2 hours trying to use an alternative solution, before finding that it's seemingly incompatible with my other dependencies.

I'm looking to use my own NodeJS server to store rooms, for the sake of having control over all rooms and not needing to depend on another service, however the example in the tutorial has a dead link: https://github.com/DadaMonad/meme-together/blob/master/server.js. I've looked through the repository and can't find any existence of the file even in the past.

Thanks!
Joe

Fill instance with data from node server before clients instance is instantiated

We want to fill an Y room on the (node) server with data before the client's instance is totally instantiated.

some details: node server: We use the web-sockets connector and an custom database adapter. The database adapter extends the memory database. The custom database adapter is used to get the data and fill the room. the client: uses web-sockets connector and memory adapter.

The problem is that we want to have data in the (server) memory BEFORE clients can get the data. To set the data in Y we need to use the transact method in the database adapter, but to use this we need to initialize the whole Y instance. This way the node server responds to the client that Y is ready while setting the data. This creates race conditions and then the client does not get all the data from the server.

what we like to have is the following: client sends to node a request to connect to the room -> node creates a new instance -> database adapter gets the data we want to use to fill the room -> database adapter fills room -> instance is ready on the node server -> node server sends room to client -> client gets the room and the data

at this moment the node server already sends an response to the client after getting the data, but before he can set the data. This is because to set the data he has to use the transact. Which only can be used if the room is ready as far I understand.

image

Is there an good way to do this? Or are we looking at this the wrong way? We like to know if there is an possibility for this, or an way to make it possible.

Huly®: YJS-39

List Type: inconsistency in error feedback

@cphyc noticed the following inconsistency:
And their may be some inconsistency in the List.val function, when you try to retrieve a negative index, it throws an error Error: this position does not exist
But when you try to get any index > 0, their's no error even though the index is undefined (then you get the last index of your array)

Note to me:
I've to check every "public" method for valid input parameters. I'll close this when I did..

Added listeners triggered too early

The callback one can set for 'addProperty' is executed too early, since the property does not exist at this time. E.g.
yatta.on('addProperty', function (ev, prop){
yatta.val(prop); //undefined
});

yatta.val(prop) is undefined.

Just saying hi

Hi there,
I'm an ex etherpad developer and have tried re-implementing a better etherpad before realizing CRDT is way better and would have had to reimplement everything were it not for this awesome project. I'm looking forward to using it.

An angle that I find intriguing is connecting yjs with scuttlebot and building applications with the two...

Here's to more decentralization! :)

re-assigning old user id

by @cphyc
And it's id is specific to one tab instance, meaning that if someone leaves then join, you get the UncaughtError: You are re-assigning an old user id - this is not (yet) possible!

Did I miss something in the tutorial?

I don't quite understand how yjs works.

I have the following code, that I run in the browser:

import Y from 'yjs'
import 'y-xmpp/y-xmpp'

const connector = new Y.XMPP({}).join('some-test-room-123546')
const y = new Y(connector) // till here everything works fine

const text = y.val('text')

if(text) console.log(text) // should happen on every other client
else y.val('text', 'some text') // should only happen on the first client

My idea was, if I use a connector that joins the same room on every client, the 'text' value would be synced across all connected clients. So on the first time text is null and the value gets created and if I open the second browser window, the value gets logged.

But somehow it always gets created. Like it was never synced with the new client.

Huly®: YJS-17

Version Header

Hi, first again thanks for the great work! :bowtie: Now to my issue: On the y-websockets-server page you describe that the Yjs version of the Web application and of the y-websockets-server have to be the same:

You have to make sure that the installed yjs package version matches the yjs version used on the client side!

However, if they are different, I get no error. This is especially confusing as I never know which y-websockets-server the "Demo" server provided by you is currently running on. It would be neat to have some kind of versioning header in the initial connection that would throw an error or at least a log message in the browser, that the Yjs versions on client and server are inconsistent.

Even if I employ my own y-websockets-server, I regularly do "bower update" before releasing the client. This occasionally updates the Yjs version, but not the one on the server.

Do you think some kind of hints would be feasible?

Allow quick switching of rooms

It would be nice to have a method like

yjs.switchRoom('roomname')

to switch the room of the current instance. An optional parameter should allow to decide, whether to copy the current state over to a not yet existing room, or to create the new room's state completely from scratch.

Huly®: YJS-22

Flush the Operations-History periodically

The operation-history is transmitted to all late-join-users. But the history gets quite fast, really huge in some applications, like collaborative drawing apps. This also causes crashes.

This issue could be handled If a history-flush method would be provided.

Huly®: YJS-4

Put creator information on delete operations

I'm currently thinking about putting creator information on delete operations. Please note that not all delete operations can contain creator information - which may lead to unexpected behavior. For efficiency reasons I can't associate a user to a delete operation forever (only for a short amount of time). Users that re-join the session won't see a creator information for that respective deletion.

Pro:

  • There are some use cases for having information about the creator of a deletion

Conn

  • This is not necessary to achieve convergence, and therefore overload.

_(Still thinking about this)_

Comments are welcome

Huly®: YJS-34

Yjs distribution

Create a Yjs distribution.

One approach would be to create another project that references to y-* repositories that are know to work together (e.g. via bower).

I'm unsure if this is a good idea (this would be one more package to maintain :(

Huly®: YJS-29

Migration status?

There is a NOTE in the readme:

This project is currently migrating. So there may exist some information that is not true anymore.

I want to use Yjs. What is the status of this migration? What should I be careful with if I start using Yjs right now?

Thank you.

Huly®: YJS-20

Support for build systems

I open this issue to discuss some problems of build systems, because I'm not very familiar with all the build systems that exist out there. Currently only globals and npm are supported. Now I plan to create support for es6 module imports too. If you know of any problems of your build process, please report them here (even if you were able to fix them).

Some people have problems using Yjs with ember-cli. Apparantly the source maps do not work correctly in the uglified code. This is a known problem and was fixed. I'll wait for the next release, update uglifyjs, and hope that this will fix the problem.

Furthermore it seems that the extention *.es6 is not recognized by broccolijs. I plan to rename the *.es6 files to *.es6.js to avoid this problem (even if .es6 is a valid extention). In order to avoid breaking automated build systems, I'll release these changes together in the next version of Yjs.

Storage agnostic implementation

Hi, Suppose you had 3 highly availability web servers being served by a load balancer, with an existing database (e.g. mongodb). How would you include yjs?

I'm assuming the main concern would be substituting mongodb in for leveldb? And then subscribing for changes from mongodb in order to update the web server version of the truth? If I've got this completely wrong then please let me know.

Thanks, John

Huly®: YJS-62

can't create new room when offline

I am trying to get truly offline-first behavior working, using the indexeddb database and either webrtc or websockets-client connector. With both of these connectors everything works fine offline with rooms which were previously created while connected. However, when disconnected from the internet and trying to create a new room with either connector, the Promise from the initial Y setup never resolves.

Y({
  db: {
    name: 'indexeddb'
  },
  connector: {
    name: 'websockets-client',
    //name: 'webrtc',
    room:  roomName
  },
  sourceDir: '/bower_components',
  share: {
    state : 'Map'
  },
  types: ['Map', 'Array']
}).then((y) => {
   // this point in the code is never reached while disconnected
})

Close XMPP connector

It should be possible to close an XMPP connection.

connector.close()

whereupon the connection to the XMPP server ends, and all memory (also regarding the Y instance) is freed.

Don't ignore null values

I recognized that yatta ignores properties which have the pointer-value 'null'.
Let's say I have an object 'a', with:

 var a = {b:10, c: null};

Now execute:

yatta.val('test', a);
yatta.val('test').val(); // {b:10}

It would be nice, if yatta wouldn't filter out the property 'c'.
The null pointer is often used to mark properties as 'inactive' or 'empty'. In my case, it is necessary to keep a valid data structure of some objects used by a external framework. My workaround looks like that:
1)set all null-pointer properties to a unique string (e.g. 'kjdnfkljd58dff58') before pushing the objects to yatta
2) setting them back to 'null' as soon as an 'addProperty' listener is fired and pass them further to the framework.

Support for PubSub communication protocol

I've been looking at the examples and was wondering whether or not support for a pubsub communication protocol would be something that could be implemented? I am interested in building a connector which would allow hooking up to a a pubsub protocol delivered over wamp (such as crossbar/autobahn). I hope this is the right spot to post this question, if not feel free to close/delete!

Huly®: YJS-45

Observe

When registering an observe function after a while, it seems that the callback function is not called. It doesn't happen if you register it 'fast' enough. I'll get back with more details when I'll find time.

Huly®: YJS-14

undefined value on late join

I have an object X with a few properties, whereat all of them are immutable. One of them is called 'text' and is type of 'string'. This property (text) is never undefined. However, if I manipulate 'text' once the following happens on late join:

  1. User joins the collaboration
  2. History buffer is sent to this user
  3. Yatta rebuilds the object X (addProperty is fired for the whole object X). At this point the property 'text' is undefined
  4. Now the manipulation operation is applied and 'text' is changed accordingly (both clients are synced)

The problem occurs at step 3. Since 'text' is never undefined (but has a default value), the application assumes that this always holds. Especially when the object X is added to the Yatta data tree.

Huly®: YJS-3

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.