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.
- The client sends
authentication.beginwith itsuserName. To open a document, it also sends thedocumentIdand, optionally,initialSelectedNodeId, the node it wants locked straight away. - The server answers
authentication.challengewith a newchallengeId, a randomnonce, and thedocumentId. A challenge can be used once, for 30 seconds. - The client answers
authentication.completewith thechallengeId, itsclientProof, and itsdocumentVersion, the newest document format it reads. - 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 protocol version (
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
| Value | Access |
|---|---|
| 0 | None: refused |
| 1 | Read |
| 2 | Edit |
| 3 | Annotate: 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:
| Field | Meaning |
|---|---|
userName, collaboratorId, access, documentId | collaboratorId identifies this connection to everyone |
documentVersion, revision, snapshot, snapshotHash | The server's current document |
heldLockNodeId | The node locked for this user from initialSelectedNodeId, or "" |
collaborators | The users in the document, this one included |
locks | [{nodeId, userName}]: nodes other users have locked |
presences | The 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:
| Field | Meaning |
|---|---|
documentId | |
operationId | A unique ID for the change. A change resent with the same ID is acknowledged again rather than applied twice. |
baseRevision | The revision the change was made from |
documentVersion | The client's document format version |
operation | The 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:
settings: the shared document settings,alignment,textAlignment,backgroundColor, andbackgroundImagewhenhasBackgroundImageis true.annotationAnchors: new positions for comments whose text moved, by annotation ID.removed: the IDs of top-level nodes to remove.nodes: top-level nodes, each in full. A node that exists is replaced where it is; a new node is inserted atindex.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:
| Answer | When |
|---|---|
document.acknowledged {documentId, operationId, revision, documentVersion} | A resent change that was already applied |
document.updated | The 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.required | The change needs a newer document format than the client reads |
| An error | Such 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.
| Message | Fields | Meaning |
|---|---|---|
node.lock.request | documentId, nodeId | Client: lock a node. The client asks for its selected node. |
node.lock.granted | documentId, nodeId | Server: the lock is this user's |
node.lock.denied | documentId, nodeId, collaboratorId, userName | Server: another user holds it |
node.lock.release | documentId, nodeId | Client: release a lock |
node.locked, node.unlocked | documentId, nodeId, collaboratorId, userName | Server, 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.
| Field | Meaning |
|---|---|
documentId, sequence | sequence goes up with each message; older ones are ignored |
x, y | The pointer, in the sender's own page coordinates |
pointerNodeId, pointerNodeX, pointerNodeY | The 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, selectedNodeIds | The selected nodes |
isEditing, selectionStart, selectionLength | Whether 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.
| Message | Fields | Meaning |
|---|---|---|
annotation.put | documentId, annotation | Client: add or change a thread, in the document format's annotation form |
annotation.delete | documentId, annotationId | Client: delete a thread |
annotation.updated | documentId, annotationId, and annotation or deleted: true | Server, 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.
| Message | Answer |
|---|---|
versions.list | versions.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
| Message | Fields | Meaning |
|---|---|---|
collaboration.user.disconnected | collaborator | A user left the document |
access.changed | documentId, access | This user's access changed; "" for the connection |
collaboration.ended | The server is ending the session; the connection closes next | |
error | code, message | Below |
Errors
| Code | Meaning |
|---|---|
invalid-message, unknown-message, protocol-version, message-too-large | The message can't be read |
invalid-authentication, invalid-challenge, authentication-failed, authentication-rate-limited, authentication-required | Signing in |
document-not-found, node-not-found | |
access-denied | The user's access doesn't allow it |
invalid-operation, invalid-lock, invalid-annotation, invalid-document-list, document-list-too-large | A 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.
| Limit | Value |
|---|---|
| Message size, and snapshot size | 8 MB |
| Presence message size | 64 KB |
| Document and node ID length | 512 characters |
| User name length | 256 characters |
| Operation and request ID length | 256 characters |
| Document list request size | 1 MB |
| Threads in a document | 5,000 |
| Comments in a thread | 500 |
| Comment length | 20,000 characters |