Skip to the content
v1.0.1-beta.17

The collaboration protocol

Pro + Collaboration. This page covers real-time collaboration, included in Scriviamo Pro + Collaboration. See pricing

This is the protocol between collaboration clients and servers, for anyone writing a server or a client of their own. Any client that follows it works with any server that follows it. The development server, src/addons/collaboration/server/collaboration-server.mjs, is a complete implementation to read alongside this page.

This page describes protocol version 6.

Messages

Messages are JSON objects sent as WebSocket text frames. Every message has the protocol version and a type:

{"protocolVersion": 6, "type": "node.lock.request", "documentId": "welcome", "nodeId": "…"}

A server answers a message with another protocol version with a protocol-version error, and an unknown type with unknown-message.

Names ending in Id are identifiers. Node IDs are a node's nodeId from the document format. Times are seconds since 1970.

Documents and snapshots

A snapshot is a whole document in the JSON document format, as a string. Its documentId must be the document's ID, and its version is the document format version.

Wherever a snapshot is sent, snapshotHash goes with it: the SHA-256 of the snapshot's UTF-8 text in hexadecimal. Clients compare it without regard to letter case, and reject a snapshot whose hash doesn't match.

Each document has a revision, which starts at 0 and goes up by one with each accepted change.

Signing in

Signing in is a challenge and response. It happens once for the connection, and again each time the client opens a document.

  1. The client sends authentication.begin with its userName. To open a document, it also sends the documentId and, optionally, initialSelectedNodeId, the node it wants locked straight away.
  2. The server answers authentication.challenge with a new challengeId, a random nonce, and the documentId. A challenge can be used once, for 30 seconds.
  3. The client answers authentication.complete with the challengeId, its clientProof, and its documentVersion, the newest document format it reads.
  4. The server checks the proof and decides the user's access. It answers authentication.accepted, or an error.

The proof is an HMAC-SHA256 in hexadecimal:

  • Key: the user's password hash, as UTF-8 text.
  • Message: these lines joined by "\n":
    • the protocol version (6);
    • the challenge ID;
    • the nonce;
    • the user name, trimmed;
    • the document ID, trimmed, or "".

The password hash is whatever the application stores for the user. Neither side sends it. The hash is used as text, so the client and server must write it identically, including letter case.

CollaborationAuthentication.createProof and verifyProof implement this.

A server should limit failed attempts. The development server allows 5 per user and address each minute.

Access

ValueAccess
0None: refused
1Read
2Edit
3Annotate: read, and add, answer and resolve comments

authentication.accepted

For the connection (no document), it has userName, access and an empty documentId.

For a document, it has:

FieldMeaning
userName, collaboratorId, access, documentIdcollaboratorId identifies this connection to everyone
documentVersion, revision, snapshot, snapshotHashThe server's current document
heldLockNodeIdThe node locked for this user from initialSelectedNodeId, or ""
collaboratorsThe users in the document, this one included
locks[{nodeId, userName}]: nodes other users have locked
presencesThe other users' latest presence messages

A collaborator is {collaboratorId, userName, documentId, access, connectedAt}.

When the client's documentVersion is older than the document's, the server answers upgrade.required with documentId and minimumDocumentVersion instead.

After accepting, the server sends collaboration.user.connected, with collaborator, to the document's other users.

Changing the document

A user with edit access sends changes as document.operation:

FieldMeaning
documentId
operationIdA unique ID for the change. A change resent with the same ID is acknowledged again rather than applied twice.
baseRevisionThe revision the change was made from
documentVersionThe client's document format version
operationThe change, below

The operation is all or nothing:

{
  "kind": "document.atomic",
  "nodes": [{"node": {"nodeId": "…", "type": "text", "content": {"text": "Hello"}}, "index": 2}],
  "removed": ["…"],
  "order": ["…", "…"],
  "settings": {"alignment": 0, "textAlignment": 0, "backgroundColor": {"red": 255, "green": 255, "blue": 255, "alpha": 0}, "hasBackgroundImage": false},
  "annotationAnchors": {"…": {"segment": 0, "start": 4, "length": 6}}
}

Every part is optional, and they apply in this order:

  1. settings: the shared document settings, alignment, textAlignment, backgroundColor, and backgroundImage when hasBackgroundImage is true.
  2. annotationAnchors: new positions for comments whose text moved, by annotation ID.
  3. removed: the IDs of top-level nodes to remove.
  4. nodes: top-level nodes, each in full. A node that exists is replaced where it is; a new node is inserted at index.
  5. order: the complete new order of the top-level node IDs.

Nodes inside containers change by sending their top-level node in full.

The server answers:

AnswerWhen
document.acknowledged {documentId, operationId, revision, documentVersion}A resent change that was already applied
document.updatedThe change was accepted. It goes to every user in the document, the sender included, with documentId, operationId, operation, revision, documentVersion, collaboratorId and userName. adjusted is true when the server dropped removals of nodes other users have locked.
document.conflict {documentId, snapshot, snapshotHash, revision, documentVersion}The change couldn't be applied; the client loads this snapshot and makes its change again
upgrade.requiredThe change needs a newer document format than the client reads
An errorSuch as access-denied without edit access

When a change is made from an older revision

When baseRevision isn't the current revision, the server still accepts the change when none of the changes since then:

  • touched the same top-level nodes;
  • changed settings, if this change does;
  • removed or reordered nodes, if this change does.

It looks back at most 64 revisions. Otherwise it answers with a conflict.

The server also answers with a conflict when a change would modify a node another user has locked.

Locks

A user must lock a node before changing it.

MessageFieldsMeaning
node.lock.requestdocumentId, nodeIdClient: lock a node. The client asks for its selected node.
node.lock.granteddocumentId, nodeIdServer: the lock is this user's
node.lock.denieddocumentId, nodeId, collaboratorId, userNameServer: another user holds it
node.lock.releasedocumentId, nodeIdClient: release a lock
node.locked, node.unlockeddocumentId, nodeId, collaboratorId, userNameServer, to the others: a node was locked or released

A user's locks are released when they leave the document or disconnect.

Presence

Clients send presence when the selection changes, and as the pointer moves, at most every 50 milliseconds. The server adds userName and collaboratorId, keeps the latest from each user, and passes it to the document's other users unchanged, including fields it doesn't know.

FieldMeaning
documentId, sequencesequence goes up with each message; older ones are ignored
x, yThe pointer, in the sender's own page coordinates
pointerNodeId, pointerNodeX, pointerNodeYThe top-level node the pointer is over or nearest to, and the pointer as fractions of that node's width and height. Receivers place the pointer from these when the node exists, and fall back to x and y.
selectedNodeId, selectedNodeIdsThe selected nodes
isEditing, selectionStart, selectionLengthWhether the user is editing text, and the text selection in characters (code points)

Presence is limited to 64 KB a message, and isn't saved.

Comments

Comments change separately from the document, so a reader with annotate access can comment.

MessageFieldsMeaning
annotation.putdocumentId, annotationClient: add or change a thread, in the document format's annotation form
annotation.deletedocumentId, annotationIdClient: delete a thread
annotation.updateddocumentId, annotationId, and annotation or deleted: trueServer, to every user: the thread as it now is

A user with edit access can make any change. A user with annotate access can add comments, delete their own, and resolve the threads they started. A refused change is answered with the stored thread, so the sender's copy goes back to it, and an access-denied error.

Versions

A server may keep versions of each document, shared with everyone in it; the versions add-on uses them. Each request has a requestId, which the answer repeats, and the document's documentId. Any user in the document may list and read versions; only users with Edit access save and delete them. A server that doesn't keep versions answers unknown-message.

MessageAnswer
versions.listversions.listed with versions, newest first: {versionId, name, createdAt, author, automatic, url}. url, when there is one, is where the version can be seen elsewhere, such as its commit.
version.save {name, automatic, readable}version.saved with the new version. The version holds the server's current document. readable, optional, is a Markdown copy for a server that keeps versions in a repository too.
version.get {versionId}version.content with versionId, snapshot and snapshotHash
version.delete {versionId}version.deleted with versionId

A request the server refuses is answered version.error with requestId, code and message, such as access-denied or version-not-found. After a save or a delete, the server sends versions.changed {documentId} to the document's other users, so they can list the versions again.

Restoring a version isn't a message: the client loads the version's snapshot into its editor and sends the difference as an ordinary change.

Git

A server that keeps an account's documents in a git repository, as Scriviamo Cloud does, says so with git: true in authentication.accepted, and the git add-on then works over the connection. Its messages have a requestId and are answered git.result {requestId, result} or git.error {requestId, code, message}: git.status, git.signIn, git.signOut, git.documents, git.open {path}, git.create {path, snapshot} and git.pullRequest {documentId}. They need authentication but no open document. A server without git answers unknown-message.

Listing documents

documents.list with a requestId and a request object asks which documents the user may open. The server answers documents.listed with the same requestId and a response, whose contents the application decides. The development server lists every document as {documentId, revision}.

Other server messages

MessageFieldsMeaning
collaboration.user.disconnectedcollaboratorA user left the document
access.changeddocumentId, accessThis user's access changed; "" for the connection
collaboration.endedThe server is ending the session; the connection closes next
errorcode, messageBelow

Errors

CodeMeaning
invalid-message, unknown-message, protocol-version, message-too-largeThe message can't be read
invalid-authentication, invalid-challenge, authentication-failed, authentication-rate-limited, authentication-requiredSigning in
document-not-found, node-not-found
access-deniedThe user's access doesn't allow it
invalid-operation, invalid-lock, invalid-annotation, invalid-document-list, document-list-too-largeA malformed request
server-error, session-ended

Limits

These are the development server's limits. The client enforces the same message size and document ID length.

LimitValue
Message size, and snapshot size8 MB
Presence message size64 KB
Document and node ID length512 characters
User name length256 characters
Operation and request ID length256 characters
Document list request size1 MB
Threads in a document5,000
Comments in a thread500
Comment length20,000 characters