Collaboration
Pro + Collaboration. This page covers real-time collaboration, included in Scriviamo Pro + Collaboration. See pricing
The collaboration add-on lets several editors work on one document at the same time, through a collaboration server. Each user sees the others' changes as they happen, along with their pointers, selections and text carets.
Load the add-on and its stylesheet:
<link rel="stylesheet" href="dist/addons/scriviamo.collaboration.css">
import {CollaborationClient, CollaborationAuthentication, CollaborationAccess} from "./dist/addons/scriviamo.collaboration.js"
How it works
- The server holds the document. Opening a shared document replaces the editor's content with the server's copy.
- A user must lock a node before changing it. The client asks for a lock on the selected node. Nodes other users have locked are locked in this editor, with the other user's name on them.
- Changes are sent as batches of changed top-level nodes, removed nodes, a new node order and document settings. The server applies a batch made from an older revision when it touched other nodes than the newer changes, and otherwise answers with a conflict and its current document.
- Other users' changes don't become undo steps, and undo never reverts them.
- Pointers, selections and carets are shared live and aren't saved.
Connecting
let client = new CollaborationClient()
client.attachEditor(editor)
client.addEventListener("connected", () => {
client.authenticate("ada", passwordHash)
})
client.addEventListener("authenticated", event => {
if (event.detail.access != CollaborationAccess.none) client.openDocument("welcome")
})
client.addEventListener("documentOpened", event => {
console.log(`Opened at revision ${event.detail.revision}`)
})
client.addEventListener("error", event => {
console.log(event.detail.code, event.detail.message)
})
client.connect("wss://example.com/collaboration")
// or: client.connect("example.com", 443, "/collaboration", true)
The steps are always: attachEditor, connect, then authenticate once connected fires,
then openDocument once authenticated fires. To stop, call disconnect() and then
detachEditor(). Detaching restores the editor's own read-only setting.
Use wss:// outside trusted networks: the client doesn't encrypt anything itself.
Signing in
The client never sends a password, or even a password hash. The server sends a one-time challenge, and the client answers with a proof made from the challenge and the password hash. The application decides how users are stored and what each may do.
authenticate(userName, passwordHash) takes the hash the server also has for that user.
CollaborationAuthentication.sha256(text) gives a SHA-256 in lowercase hexadecimal.
The proof is keyed by the hash text, so the client and server must agree on its exact form, including letter case. If your server stores hashes in uppercase hexadecimal, uppercase the client's hash to match:
let passwordHash = CollaborationAuthentication.sha256(password).toUpperCase()
Access
The server gives each user one level of access to a document:
CollaborationAccess | The user can |
|---|---|
none | Nothing; authentication failed or the user may not open the document |
read | View the document, pointers and selections |
annotate | View the document, and add, answer and resolve comments |
edit | Change the document and its comments |
With less than edit access, the editor is read-only. The server can change a user's access
while they are connected, which raises accessChanged.
The client
| Member | Meaning |
|---|---|
isConnected, isAuthenticated | |
access, documentID, revision | The open document, this user's access, and the revision the editor shows |
user | This user, as a CollaborationUser |
heldLockNodeID | The node this user has locked, or "" |
hasUnconfirmedChanges | True while changes are waiting to be sent or confirmed |
requestDocuments(request) | Asks the server which documents this user may open; the answer arrives as documentsReceived |
Client events
| Event | detail | When |
|---|---|---|
connected, disconnected | The connection opened or closed | |
authenticated | {userName, access} | After signing in |
documentOpened | {documentID, access, revision} | After the server's copy was loaded |
documentUpdated | {documentID, revision, collaborator} | After any accepted change, local or remote |
conflictResolved | {documentID, revision} | After recovering from a conflict |
offlineChangesDetected | {documentID, localSnapshot, serverSnapshot} | When local content was replaced by the server's |
offlineChangesMerged | {documentID, kept, conflicts} | After changes made while disconnected were merged into the server's copy; see Working offline |
versionsChanged | {documentID} | When another user saved or deleted a version; see Version history |
accessChanged | {documentID, access} | When the server changed this user's access |
nodeLockDenied | {nodeID, node, collaborator} | When another user holds the selected node |
presenceReceived | {collaborator} | For each pointer or selection update |
documentsReceived | {requestID, response} | Answering requestDocuments |
upgradeRequired | {documentID, minimumDocumentVersion} | When the document needs a newer editor |
error | {code, message} |
Working offline
When the connection drops, the editor stays editable. Changes made while disconnected are kept, and when the client opens the document again, they're merged into the server's copy, which other users may have changed meanwhile, and sent as one update. The merge goes block by block, against the copy the changes were made from:
- a block only this user changed, added or removed takes their change;
- a block others changed too, or that another user has locked, keeps the server's version, and is
reported in
offlineChangesMerged'sconflicts, each with this user's version aslocal, so the application can show or restore it; - blocks added offline go after the block before them; when only this user reordered blocks, their order is kept, and otherwise the server's is;
- document settings follow the same rules.
client.addEventListener("offlineChangesMerged", event => {
let {kept, conflicts} = event.detail
if (conflicts.length > 0) showConflicts(conflicts) // your own interface
})
client.addEventListener("disconnected", () => setTimeout(reconnect, 2000)) // your own reconnecting
The client doesn't reconnect on its own: connect, sign in and open the document again, as at the start. To keep offline changes when the page closes before the connection returns, give the client somewhere to store them; they're merged the same way the next time the document opens:
client.offlineStorage = localStorage
The stored copy holds the document's text in the browser, so leave offlineStorage unset on
shared computers. Set mergeOfflineChanges to false for the earlier behavior: the server's copy
replaces changes made offline, which offlineChangesDetected reports.
mergeOfflineChanges(base, local, server, locked), exported by the add-on, is the merge itself,
for applications that keep documents some other way.
Versions and suggestions
With the versions add-on, versions are kept by the collaboration server while a document is open,
and everyone in it sees the same ones; client.versionStore is the store it uses. With the track
changes add-on, suggested changes are part of the document, so everyone sees and resolves them.
See Version history and Track changes.
The editor while collaborating
The add-on adds these to every editor:
| Member | Meaning |
|---|---|
isCollaborating | True while a shared document is open |
collaborationUser | This editor's user, or null |
collaborators() | Everyone in the document, this user included, as CollaborationUsers |
collaborationClient | The attached client, or null |
collaborationLockOwner(node) | The name of the user who has a node locked, or "" |
A CollaborationUser has a userName, access, color (the same for the same name in every
editor), selectedNodeIDs(), isEditing, and the user's pointer. The pointer is kept relative
to the top-level node under it (pointerNodeID, pointerNodeX, pointerNodeY, as fractions of
that node's size), so it appears over the same content even when editors are different widths.
Editor events
| Event | detail | When |
|---|---|---|
collaborationConnected | {user} | After a shared document opened |
collaborationDisconnected | After it closed | |
collaborationUserConnected, collaborationUserDisconnected | {collaborator} | As other users join and leave |
remoteDocumentChanged | {collaborator, revision} | After another user's change was applied |
remoteNodeLockChanged | {collaborator, node, nodeID, locked} | When another user locks or releases a node |
remotePointerMoved | {collaborator, node, x, y} | When another user's pointer moves |
remoteSelectionChanged | {collaborator, node} | When another user selects a node; node is null when they clear the selection |
The development server
src/addons/collaboration/server/collaboration-server.mjs is a small collaboration server for
development and testing. It needs only Node.js.
node src/addons/collaboration/server/collaboration-server.mjs --port 8770
| Option | Default |
|---|---|
--port | 8770 |
--path | /collaboration |
--root | The repository root, the folder it serves pages from |
--trace | Logs every message |
It serves the demo page at http://localhost:8770/ and collaboration at
ws://localhost:8770/collaboration. Its demo document is welcome.
Demo users all have the password password. Ada and Grace may edit, Alan may comment, Linus
may read, and any other name may edit.
It keeps documents in memory only, has no TLS, and its users are fixed, so it isn't for production. It follows the same protocol rules as a production server, so any compatible server can replace it. The collaboration protocol describes those rules for writing a server of your own.
Requests from the same computer can also use these development endpoints:
| Request | Does |
|---|---|
GET /collaboration/sessions | Lists the connected users |
POST /collaboration/access {collaboratorId, access} | Changes a user's access |
POST /collaboration/disconnect {collaboratorId, clearDocument} | Disconnects a user |
POST /collaboration/documents {documentId, snapshot} | Adds or replaces a document |
Trying it in the demo
Run the server, open http://localhost:8770/, and click Connect… above the editor. The
dialog is filled in for that server. Open the page in a second window to see two users at
once. The log under the editor lists what happens.