- Notifications
You must be signed in to change notification settings - Fork9
Share and edit documents in real-time: transport-agnostic operational transformation control layer
License
gulf/gulf
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Operational transformation is a set of algorithms that allow you to sync documents. It's what Google Docs and Etherpad use for real-time collaboration. Gulf is a unixy ("do one thing and do it well") take on collaborative editing, by connecting documents with Node.js streams and employing OT to resolve conflicts automatically.
This project evolved from my determination to replace Etherpad with something better. Gulf was the smallest core of code that I felt comfortable extracting out of my prototypes. Unfortunately, I took a lot of detours before I could finish "something better" and eventually discovered something better than OT itself: CRDT. Gulf is thus relic, of sorts.
Nonetheless, it's finished and works splendidly. (The tests are failing due to entropy, which I haven't bothered to account for.) So, if you really need OT: Happy syncing!
/* * Server */vartextOT=require('ot-text').type// Create a new master documentvardoc=newgulf.Document({storageAdapter:newgulf.MemoryAdapter,ottype:textOT})doc.initializeFromStorage('abc')// Optionally supply default content// Set up a serverws.createServer(function(socket){// ... and create a slave link for each socket that connectsvarslave=doc.slaveLink()// now, add the new client as a slavesocket.pipe(slave).pipe(socket)})
/* * Browser */vartextOT=require('ot-text').type// Create a new *editable* slave document (empty by default)vardoc=newgulf.EditableDocument({storageAdapter:newgulf.MemoryAdapter,ottype:textOT})// Implement editor bindingsdoc._onBeforeChange=function(){/*...*/}doc._onChange=function(){/*...*/}doc._setContent=function(){/*...*/}// Connect to alice's serverws.connect(function(socket){// create a link to the mastervarmaster=doc.masterLink()// connect to master documentsocket.pipe(master).pipe(socket)})
And you have a collaborative editor!
A document may contain arbitrary data. The content of a document is available inmyDocument.content
(which is read-only!).
Now, how do I change this document ifDocument#content
is untouchable? Well, thankfully there's also EditableDocuments.
Editable documents can be updated via thesubmitChange(cs)
method. Thecs
is short for changeset. A changeset contains the changes to a document.
EditableDocuments leave some methods for you to implement:
vardocument=newgulf.EditableDocument(adapter,ottype)document._onChange=function(cs,cb){/* apply changes return promise */}document._setContent=function(newcontent,cb){/* set new content return promise */}document._onBeforeChange=functioncb(){/* collect changes and submitChange() them return promise */}
Before anything can happen, the editable document is initialized wwith_setContent
.
Everytime the document is changed by an incoming edit_onChange
is called with the changeset.
Before an incoming edit is processed_onBeforeChange
is called, allowing you to save possible changes before the new edit is applied. Just like you'd git commit before git pulling anything -- we don't want things would get hairy.
There are two ways to get a changeset: 1) you compute a diff between the last known state and the current one. 2) You record edit events and turn them into a changeset. There are editors which provide changesets out-of-the-box, others will have to be persuaded (using diff computation).
Ok, now we update our editable document and we notice that it keeps a record of all revisions -- documents remember every change ever done. Nice.
Now, Alice and Bob each have an editable document and want to sync them. For this, we need some kind of mediator document that takes care of the syncing process and representsthe absolute truth. In gulf this mediator is called the master document. It has the final say in which edit is accepted and how the edits are ordered.
Now, somehow Alice and Bob need to link their documents to that master document in order to send it the changes they make.
For this gulf provides, surprise, Links. A Link is a simple DuplexStream. If Alice wants to connect to the master document, she creates a master link to it. The master document attaches Alice's link as a slave link.
net.createserver((socket)=>{socket.pipe(masterDoc.slaveLink()).pipe(socket)// for each socket}).listen(1234)
net.connect(1234,function(er,socket){socket.pipe(slaveDoc.masterLink()).pipe(socket)})
A document can have many slaves, but only one master link (EditableDocuments have no slave links).
Now that we've connected all documents, every time Alice or Bob make a change the edits will just flow to the other documents.
You can sync any document type you have an ot type implementation and an editor for.
Since adding gulf syncing to an editor is a repetitive task and hard to get right (what with selection retention, generating diffs, etc.) there are ready-made bindings for you!
The following bindings are available:
- contenteditable (using DOM OT; eg. CKeditor)
- textarea/textinput (using text OT)
- codemirror (using text OT)
- socialcalc (using socialcalc OT)
- quill (using rich text OT)
If you want to create a binding yourself, please follow the API of the existing modules (ie. expose a single class extending EditableDocument and taking an additional option callededitorInstance
. And don't forget to implementEditableDocument#close()
!). Also, make sure to name the npm package like this:gulf-editor-your-name-here
Gulf allows you to store your data anywhere you like, if you can provide it with a storage adapter. It comes with an in-memory adapter, ready for you to test your app quickly, but when the time comes to get ready for production you will want to change to a persistent storage backend like mongoDB or redis.
Currently implemented adapters are:
If you'd like to write your own storage adapter, head on to the API docs below and be sure to name it like this:gulf-backend-your-name-here
It's probably easiest to observe gulf in action. So, have a look at these examples.
- https://gist.github.com/marcelklehr/0430be7e3fb45a83189b -- a small html page with two contenteditables that are synced.
- https://github.com/marcelklehr/warp -- a complete web server serving a collaborative editor, driven by CKeditor, sockJS and gulf-contenteditable
- http://openevocracy.org/ -- a new take on democratic decision-making, with gulf as the real-time editing engine.
Additions wanted: If you have the perfect example show-casing gulf or its related libraries leave me a note via email orthe issues.
Instantiates a new link, optionally with some options:
opts.credentials
The credentials to be sent to the other end for authentication purposes.opts.authenticate
A functon which gets called with the credentials from the other side and has the following signature:(credentials): Promise<Object>
opts.authorizeWrite
A function which gets called when the other end writes a message, and has the following signature:(msg, user): Promise<Bool>
;user
is the value returned by yourauthenticate
hook.opts.authorizeRead
A function which gets called when this side of the link writes a message, and has the following signature:(msg, user): Promise<Bool>
;user
is the value returned by yourauthenticate
hook.
The return value ofopts.authenticate
is also used as the author field when saving snapshots.
Here's an example of how to setup link authentication and authorization:
varlink=newgulf.Link({authenticate:function(credentials){returnauthenticate('token',credentials).then((user)=>{returnuser.id})},authorizeWrite:function(msg,userId,cb){switch(msg[0]){case'edit':returnauthorize(userId,'document:change').then(auth=>auth.granted)case'requestInit':returnauthorize(userId,'document:read').then(auth=>auth.granted)}},authorizeRead:function(msg,userId,cb){switch(msg[0]){case'init':case'edit':returnauthorize(userId,'document:read').then(auth=>auth.granted)case'ack':returnauthorize(userId,'document:change').then(auth=>auth.granted)}}})
Fired when the document has received aninit
message containing a snapshot, has reset the history and set the new contents.
Fired when an edit has been committed (confirmed with master, applied locally and stored).ownEdit
tells you ifedit
was submitted by this document or was received from a different document.
Instantiates a new, empty Document.storageAdapter
is optional and defaults to a new instance ofgulf.MemoryAdapter
Loads a document from the storage.opts
will be passed to the Document constructor.
Creates a link withopts
passed to the Link constructor and attaches it as a slave link.
Creates a link withopts
passed to the Link constructor and attaches it as a master link.
Attaches an existing link as master.
Attaches an existing link as a slave.
Listener function that gets called when a link attached to this document receives aninit
message.data
could look like this:{contents: 'abc', edit: '<Edit>'}
A listener function that gets called when a link receives anedit
message. Adds the edit to the queue (after checking with a possible master) and calls Document#dispatchEdit() when ready.
Checks with the document's History whether we know this edit already, and if not, whether we know its parent. If so, it calls Document#sanitizeEdit(), applies the edit to this document with Document#applyEdit(), add it to this document's History, send's anack
message to the link the edit came from, distributes the edit to any slaves and emits anedit
event.
Transforms the passed edit against missed edits according to this document's history and the edit's parent information.
Applies an edit to the document's content.
Sends the passed edit to all attached links, except tofromLink
.
This class extendsgulf.Document
and overrides some of its methods.
The following are the defaults for the options (justmergeQueue
at this time):
{ mergeQueue: true // If gulf should merge multiple outstanding edits into one, for faster collaboration.}
Fired when gulf has received the init packet and has set the contents viaEditableDocument#_setContent
.
Fired when an edit has been committed (confirmed with master, applied locally and stored).ownEdit
tells you ifedit
was submitted by this document or was received from a different document.
Update an editable document with local changes provided inchanges
. This wraps the changes in an Edit and sends them to master.
Fired when EditableDocument#update() has been called, but before the changes have been approved by the master.edit
is the newly created Edit.
Note: If queue merging is enabled, the supplied edit may be merged with other outstanding edits before being sent to the server. Thus, if queue merging is enabled, it is not guaranteed that you will get acommit
event for every edit that you got anupdate
event for.
An EditableDocument consumer can call this to tear down the connection between EditableDocument and editor.
Needs to be implemented by you or by wrappers (seeEditor bindings). Is called after the document has been initialized with_setContents
for every change that is received from master.
Needs to be implemented by you or by wrappers (seeEditor bindings). Is called if the document receives aninit
message or to reset the document in case of an error.
Needs to be implemented by you or by wrappers (seeEditor bindings). Is called right before_onChange()
is called to keep track of any outstanding changes.
instantiates a new edit without parent, changes or id. Thus it's pretty useless.
Deserializes an edit that was serialized usinggulf.Revision#toJSON()
.
Creates a new initial edit. Initial revisions carry content but no changes.
Creates a new edit with changes set tocs
.
Applies this edit on a document snapshot.
transforms this edit against the passed one and sets the other as this edit's parent. (This operation rewrites history.)
transforms this edit against the passed one and without resetting this edit's parent.
merges the passed edit with this one. Returns the new edit.
Serializes this edit.
Returns a new edit instance that has exactly the same properties as this one.
Gulf storage adapters are provided in npm packages that are namedgulf-backend-yournamehere
.They deal with revision objects.A revision is an object that looks like this:
{id:48,changeset:[0,"h"],parent:47// ID of this revision's parent,content:'"Hello world"'// stringified representation of the new contents,author:12// The id of the author, as returned by `opts.authenticate` in the Link options (or the value you passed to gulf.Document#receiveEdit, if you passed in the edit directly)}
If you're having trouble writing your own adapter, check outthe in-memory adapter and theblob store adapter.
Adapter#getRevision(revId:Number) : Promise
Tests?
Test-it-yourself
To run the tests in node and the browser, run the following:
npm run build && npm run test-local
(Make sure to open the provided link in the browser of your choice.)
License
(c) 2013-2016 by Marcel Klehr
GNU Lesser General Public License
Changelog
v5.0.0
- Complete refactoring and API overhaul
v4.1.0
- EditableDoc: add Event
editableInitialized
- EditableDoc: add Event
commit
- Add option
mergeQueue
v4.0.5
- Fix memory leak: Doc#detachLink on Link:'finish'
v4.0.4
- Fix reconnect catching-up
v4.0.3
- Clear sentEdit timeout on Link#reset
- Remove pointless guard in Link#send
v4.0.2
- Fix createDocument. history wasn't passing on doc.id
- Change code header: LGPL
v4.0.1
- License under LGPL 🎆
v4.0.0
- Revamp Adapter interface
- Add author to snapshots: Use link.authenticated as author value
- Implement commit retry: Default edit sending timeout is 10s
- Revamp auth: Clearly separate authentication from authorization
About
Share and edit documents in real-time: transport-agnostic operational transformation control layer