tiptap/docs/src/docPages/guide/collaborative-editing.md

326 lines
11 KiB
Markdown
Raw Normal View History

2020-10-05 20:56:45 +08:00
# Collaborative editing
2020-11-28 00:23:04 +08:00
:::premium Become a sponsor
Using collaborative editing in production? Do the right thing and [sponsor our work](/sponsor)!
:::
2020-11-30 21:27:36 +08:00
<!--
TODO:
- Pass auth token to the provider
-
-->
## toc
2020-10-05 20:56:45 +08:00
## Introduction
2020-11-28 05:17:03 +08:00
Real-time collaboration, syncing between different devices and working offline used to be hard. We provide everything you need to keep everything in sync, conflict-free with the power of [Y.js](https://github.com/yjs/yjs). The following guide explains all things to take into account when you consider to make tiptap collaborative. Dont worry, a production-grade setup doesnt require much code.
2020-10-05 20:56:45 +08:00
## Configure collaboration
2020-11-28 00:23:04 +08:00
The underyling schema tiptap uses is an excellent foundation to sync documents. With the [`Collaboration`](/api/extensions/collaboration) you can tell tiptap to track changes to the document with [Y.js](https://github.com/yjs/yjs).
2020-10-05 20:56:45 +08:00
2020-11-28 00:23:04 +08:00
Y.js is a conflict-free replicated data types implementation, or in other words: Its reaaally good in merging changes. And to achieve that, changes dont have to come in order. Its totally fine to change a document while being offline and merge the it with other changes when the device is online again.
2020-10-05 20:56:45 +08:00
2020-11-28 00:23:04 +08:00
But somehow, the clients need to interchange document modifications. The most technologies used to do that are WebRTC and WebSocket, so lets have a look those:
### WebRTC
Anyway, lets take the first steps. Install the dependencies:
```bash
# with npm
npm install @tiptap/extension-collaboration yjs y-webrtc
# with Yarn
yarn add @tiptap/extension-collaboration yjs y-webrtc
```
2020-11-30 21:27:36 +08:00
Now, create a new Y document, and register it with tiptap:
2020-11-28 00:23:04 +08:00
```js
import { Editor } from '@tiptap/core'
import Collaboration from '@tiptap/extension-collaboration'
import * as Y from 'yjs'
import { WebrtcProvider } from 'y-webrtc'
// A new Y document
const ydoc = new Y.Doc()
// Registered with a WebRTC provider
const provider = new WebrtcProvider('example-document', ydoc)
const editor = new Editor({
extensions: [
// …
// Register the document with tiptap
Collaboration.configure({
2020-11-30 21:27:36 +08:00
provider
2020-11-28 00:23:04 +08:00
}),
],
})
```
2020-11-28 05:17:03 +08:00
This should be enough to create a collaborative instance of tiptap. Crazy, isnt it? Try it out, and open the editor in two different browsers. Changes should be synced between different windows.
2020-11-28 00:23:04 +08:00
2020-11-28 05:17:03 +08:00
So how does this magic work? All clients need to connect with eachother, thats the job of providers. The [WebRTC](https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API) provider is the easiest way to get started with, as it requires a public server to connect clients directly with-each other, but not to sync the actual changes. This has two downsides, though.
2020-11-28 00:23:04 +08:00
2020-11-28 05:17:03 +08:00
On the one hand, browsers refuse to connect with too many clients. With Y.js its enough if all clients are connected indirectly, but even that isnt possible at some point. Or in other words, it doesnt scale well for more than 100+ clients in the same document.
2020-11-28 00:23:04 +08:00
2020-11-28 05:17:03 +08:00
On the other hand, its likely you want to involve a server to persist changes anyway. But the WebRTC signaling server (which connects all clients with eachother) doesnt receive the changes and therefore doesnt know whats in the document.
2020-11-28 00:23:04 +08:00
2020-11-28 05:17:03 +08:00
Anyway, if you want to dive deeper, head over to [the Y WebRTC repository](https://github.com/yjs/y-webrtc) on GitHub.
2020-11-28 00:23:04 +08:00
### WebSocket (Recommended)
For most uses cases, the WebSocket provider is the recommended choice. Its very flexible and can scale very well. For the client, the example is nearly the same, only the provider is different. Install the dependencies first:
```bash
# with npm
npm install @tiptap/extension-collaboration yjs y-websocket
# with Yarn
yarn add @tiptap/extension-collaboration yjs y-websocket
```
And then register the WebSocket provider with tiptap:
```js
import { Editor } from '@tiptap/core'
import Collaboration from '@tiptap/extension-collaboration'
import * as Y from 'yjs'
import { WebsocketProvider } from 'y-websocket'
// A new Y document
const ydoc = new Y.Doc()
// Registered with a WebSocket provider
const provider = new WebsocketProvider('ws://127.0.0.1:1234', 'example-document', ydoc)
const editor = new Editor({
extensions: [
// …
// Register the document with tiptap
Collaboration.configure({
2020-11-30 21:27:36 +08:00
provider
2020-11-28 00:23:04 +08:00
}),
],
})
```
2020-11-28 05:17:03 +08:00
That example doesnt work out of the box. As you can see, its configured to talk to a WebSocket server which is available under `ws://127.0.0.1:1234` (WebSocket protocol, your local IP and port 1234). You need to set this up, too.
2020-11-28 00:23:04 +08:00
2020-11-28 05:17:03 +08:00
To make the server part as easy as possible, we provide you with an opinionated server package, called [hocuspocus](http://github.com/ueberdosis/hocuspocus). Create a new project, and install the hocuspocus server as a dependency:
2020-11-28 00:23:04 +08:00
```bash
# with npm
npm install @hocuspocus/server
# with Yarn
yarn add @hocuspocus/server
```
2020-11-28 05:17:03 +08:00
Create an `index.js` and throw in the following content, to create, configure and start your very own WebSocket server:
2020-11-28 00:23:04 +08:00
```js
import { Server } from '@hocuspocus/server'
const server = Server.configure({
port: 1234,
})
server.listen()
```
2020-11-28 05:17:03 +08:00
Thats all. Start the script with:
2020-11-28 00:23:04 +08:00
```bash
node ./index.js
```
2020-11-28 05:17:03 +08:00
This should output something like “Listening on ws://127.0.0.1:1234”. If you go back to your tiptap editor and hit reload, it should connect to the WebSocket server and changes should sync with all other clients. Amazing, isnt it?
2020-10-05 20:56:45 +08:00
### Add cursors
2020-11-28 00:23:04 +08:00
If you want to enable users to see the cursor and text selections of each other, add the [`CollaborationCursor`](/api/extensions/collaboration-cursor) extension.
```js
import { Editor } from '@tiptap/core'
import Collaboration from '@tiptap/extension-collaboration'
import CollaborationCursor from '@tiptap/extension-collaboration-cursor'
import * as Y from 'yjs'
import { WebsocketProvider } from 'y-websocket'
const ydoc = new Y.Doc()
const provider = new WebsocketProvider('ws://127.0.0.1:1234', 'example-document', ydoc)
const editor = new Editor({
extensions: [
// …
Collaboration.configure({
2020-11-30 21:27:36 +08:00
provider
2020-11-28 00:23:04 +08:00
}),
// Register the collaboration cursor extension
CollaborationCursor.configure({
provider: this.provider,
name: 'Cyndi Lauper',
color: '#f783ac',
}),
],
})
```
2020-11-30 21:44:18 +08:00
As you can see, you can pass a name and color for every user. Look at the [collaborative editing example](/examples/collaborative-editing), to see a more advanced example.
2020-10-05 20:56:45 +08:00
### Offline support
2020-11-28 05:17:03 +08:00
Adding offline support to your collaborative editor is basically a one-liner, thanks to the fantastic [Y IndexedDB adapter](https://github.com/yjs/y-indexeddb). Install it:
2020-11-28 00:23:04 +08:00
```bash
# with npm
npm install y-indexeddb
# with Yarn
yarn add y-indexeddb
```
And connect it with a Y document:
```js
import { Editor } from '@tiptap/core'
import Collaboration from '@tiptap/extension-collaboration'
import * as Y from 'yjs'
import { IndexeddbPersistence } from 'y-indexeddb'
const ydoc = new Y.Doc()
// Store the Y document in the browser
const indexdb = new IndexeddbPersistence('example-document', ydoc)
const editor = new Editor({
extensions: [
// …
Collaboration.configure({
2020-11-30 21:27:36 +08:00
provider
2020-11-28 00:23:04 +08:00
}),
],
})
```
2020-11-28 05:17:03 +08:00
All changes will be stored in the browser then, even if you close the tab, go offline, or make changes while working offline. Next time you are online, the WebSocket provider will try to find a connection and eventually sync the changes.
2020-11-28 00:23:04 +08:00
2020-11-28 05:17:03 +08:00
Yes, its magic. As already mentioned, that is all based on the fantastic Y.js framework. And if youre using it, or our integration, you should definitely [sponsor Kevin Jahns on GitHub](https://github.com/dmonad), he is the brain behind Y.js.
2020-10-05 20:56:45 +08:00
## Store the content
2020-11-28 05:17:03 +08:00
Our collaborative editing backend is ready to handle advanced use cases, like authorization, persistence and scaling. Lets go through a few common use cases here!
2020-11-28 00:23:04 +08:00
2020-12-02 22:35:41 +08:00
### Authentication
With the `onConnect` hook you can write a custom Promise to check if a client is authenticated. That can be a request to an API, to a microservice, a database query, or whatever is needed, as long as its executing `resolve()` at some point. You can also pass contextual data to the `resolve()` method which will be accessible in other hooks.
```js
import { Server } from '@hocuspocus/server'
const server = Server.configure({
onConnect(data, resolve, reject) {
const { requestHeaders } = data
// Your code here, for example a request to an API
// If the user is not authorized …
if (requestHeaders.access_token !== 'super-secret-token') {
return reject()
}
// Set contextual data
const context = {
user_id: 1234,
}
// If the user is authorized …
resolve(context)
},
})
server.listen()
```
2020-11-28 00:23:04 +08:00
### Authorization
2020-12-02 22:35:41 +08:00
With the `onJoinDocument` hook you can check if a user is authorized to edit the current document. This works in the same way the [Authentication](#authentication) works.
2020-11-28 00:23:04 +08:00
```js
import { Server } from '@hocuspocus/server'
const server = Server.configure({
onJoinDocument(data, resolve, reject) {
2020-12-02 22:35:41 +08:00
const {
clientsCount,
context,
document,
documentName,
requestHeaders,
} = data
2020-11-28 00:23:04 +08:00
// Your code here, for example a request to an API
2020-12-02 22:35:41 +08:00
// Access the contextual data from the onConnect hook, in this example this will print { user_id: 1234 }
console.log(context)
2020-11-28 00:23:04 +08:00
// If the user is authorized …
resolve()
// if the user isnt authorized …
reject()
},
})
server.listen()
```
### Persist the document
2020-11-28 05:17:03 +08:00
By default, documents are only stored in the memory. Hence they are deleted when the WebSocket server is stopped. To prevent this, store changes on the hard disk with the LevelDB adapter. When you restart the server, itll restore documents from the hard disk, in that case from the `./database` folder:
2020-11-28 00:23:04 +08:00
```js
import { Server } from '@hocuspocus/server'
import { LevelDB } from '@hocuspocus/leveldb'
const server = Server.configure({
persistence: new LevelDB({
path: './database',
}),
})
server.listen()
```
### Send it to an API
To pass the updated documents to an API, or to a database, you can use the `onChange` hook, which is executed when a document changes. With the `debounce` setting you can slow down the execution, with the `debounceMaxWait` setting you can make sure the content is sent at least every few seconds:
2020-11-28 00:23:04 +08:00
```js
import { Server } from '@hocuspocus/server'
const server = Server.configure({
// time to wait before sending changes (in milliseconds)
debounce: 2000,
// maximum time to wait (in milliseconds)
debounceMaxWait: 10000,
2020-11-28 00:23:04 +08:00
// executed when the document is changed
onChange(data) {
2020-12-02 22:35:41 +08:00
const {
clientsCount,
document,
documentName,
requestHeaders,
} = data
2020-11-28 00:23:04 +08:00
2020-12-02 22:35:41 +08:00
// Your code here, for example a request to an API
2020-11-28 00:23:04 +08:00
},
})
server.listen()
```
### Scale with Redis (Advanced)
2020-11-28 05:17:03 +08:00
To scale the WebSocket server, you can spawn multiple instances of the server behind a load balancer and sync changes between the instances through Redis. Install the Redis adapter and register it with hocuspocus:
2020-10-05 20:56:45 +08:00
2020-11-28 00:23:04 +08:00
```js
import { Server } from '@hocuspocus/server'
import { Redis } from '@hocuspocus/redis'
2020-10-05 20:56:45 +08:00
2020-11-28 00:23:04 +08:00
const server = Server.configure({
persistence: new Redis('redis://:password@127.0.0.1:1234/0'),
})
2020-10-05 20:56:45 +08:00
2020-11-28 00:23:04 +08:00
server.listen()
```