tiptap/docs/cloud.md

197 lines
8.5 KiB
Markdown
Raw Normal View History

---
tableOfContents: true
---
2023-06-03 00:26:24 +08:00
# Tiptap Collab
2023-08-05 07:45:55 +08:00
Implementing real-time collaboration is quite hard. With Tiptap Collab we build a solution that does it in minutes. To see it in action check out our [live demo](https://tiptap.dev/editor).
Tiptap Collab is our managed cloud solution of [Hocuspocus](https://tiptap.dev/hocuspocus/introduction). It makes it a easy to add real-time collaboration to any application. If you already have an application using Tiptap Editor, it's even easier to add collaboration.
:::warning Pro Feature
2023-08-05 07:45:55 +08:00
To get started, you need a Tiptap Pro account. [Log in](https://tiptap.dev/login) or [sign up](https://tiptap.dev/register) for free.
:::
2023-06-03 00:26:24 +08:00
2023-08-05 07:45:55 +08:00
## Getting Started
2023-06-03 00:26:24 +08:00
2023-08-05 07:45:55 +08:00
### Installation
2023-06-03 00:26:24 +08:00
2023-08-05 07:45:55 +08:00
First you need to install `@hocuspocus/provider` at least in version `2.0.0`.
2023-06-03 00:26:24 +08:00
2023-08-05 07:45:55 +08:00
```bash
npm install @hocuspocus/provider
```
2023-06-03 00:26:24 +08:00
2023-08-05 07:45:55 +08:00
### Basic Usage
2023-06-03 00:26:24 +08:00
2023-08-05 07:45:55 +08:00
Tiptap Collab makes your application collaborative by synchronizing a Yjs document between connected users using websockets. If you're already using Yjs in your application, it's as easy as this:
2023-06-03 00:26:24 +08:00
```typescript
import { TiptapCollabProvider } from '@hocuspocus/provider'
import * as Y from 'yjs'
const provider = new TiptapCollabProvider({
appId: 'your_app_id', // get this at collab.tiptap.dev
name: 'your_document_name', // e.g. a uuid uuidv4();
token: 'your_JWT', // see "Authentication" below
document: new Y.Doc() // pass your existing doc, or leave this out and use provider.document
2023-06-03 00:26:24 +08:00
});
```
2023-08-05 07:45:55 +08:00
### Upgrade From Hocuspocus
2023-06-03 00:26:24 +08:00
2023-08-05 07:45:55 +08:00
If you are upgrading from our self-hosted collaboration backend called Hocuspocus, all you need to do is replace `HocuspocusProvider` with the new `TiptapCollabProvider`. The API is the same, it's just a wrapper that handles the hostname to your Tiptap Collab app and authentication.
2023-06-03 00:26:24 +08:00
2023-08-05 07:45:55 +08:00
## Example
2023-06-03 00:26:24 +08:00
[![Cloud Documents](https://tiptap.dev/images/docs/server/cloud/tiptapcollab-demo.png)](https://tiptap.dev/images/docs/server/cloud/tiptapcollab-demo.png)
2023-08-05 07:45:55 +08:00
We have created a simple client / server setup using replit that you can review and fork here:
[Github](https://github.com/janthurau/TiptapCollab) or [Replit Demo](https://replit.com/@ueberdosis/TiptapCollab?v=1)
The example loads multiple documents over the same websocket (multiplexing), and shows how to implement per-document authentication using JWT.
2023-06-03 00:26:24 +08:00
2023-08-05 07:45:55 +08:00
More tutorials can be found in our [Tutorials section](/tutorials).
2023-06-03 00:26:24 +08:00
2023-08-05 07:45:55 +08:00
## Authentication
2023-06-03 00:26:24 +08:00
2023-08-05 07:45:55 +08:00
Authentication is done using [JSON Web Token (JWT)](https://en.wikipedia.org/wiki/JSON_Web_Token). There are many libraries available to generate a valid token.
2023-06-03 00:26:24 +08:00
2023-08-05 07:45:55 +08:00
### JWT Generation
To generate a JWT in the browser, you can use [http://jwtbuilder.jamiekurtz.com/](http://jwtbuilder.jamiekurtz.com/). You can leave all the fields as default, just replace the "Key" at the bottom with the secret from your [settings](https://collab.tiptap.dev/apps/settings).
2023-06-03 00:26:24 +08:00
In Node.js, you can generate a JWT like this:
```typescript
import jsonwebtoken from 'jsonwebtoken'
const data = {
2023-08-05 07:45:55 +08:00
// Use this list to limit the number of documents that can be accessed by this client.
// An empty array means no access at all.
// Not sending this property means access to all documents.
// We are supporting a wildcard at the end of the string (only there).
2023-06-03 00:26:24 +08:00
allowedDocumentNames: ['document-1', 'document-2', 'my-user-uuid/*', 'my-organization-uuid/*']
}
2023-08-05 07:45:55 +08:00
// This JWT should be sent in the `token` field of the provider. Never expose 'your_secret' to a frontend!
2023-06-03 00:26:24 +08:00
const jwt = jsonwebtoken.sign(data, 'your_secret')
```
2023-08-05 07:45:55 +08:00
## Webhook
You can define a URL and we will call it every time a document has changed. This is useful for getting the JSON representation of the Yjs document in your own application. We call your webhook URL when the document is saved to our database. This operation is debounced by 2-10 seconds. So your application won't be flooded by us. Right now we're only exporting the fragment `default` of the Yjs document.
You can add the webhook URL in the [settings page](https://collab.tiptap.dev/apps/settings) of your Tiptap Collab app.
2023-06-03 00:26:24 +08:00
2023-08-05 07:45:55 +08:00
### Payload
2023-06-03 00:26:24 +08:00
2023-08-05 07:45:55 +08:00
A sample payload of the webhook request looks like this:
2023-06-03 00:26:24 +08:00
```json
{
"appName": '', // name of your app
"name": '', // name of the document
"time": // current time as ISOString (new Date()).toISOString())
"tiptapJson": {}, // JSON output from Tiptap (see https://tiptap.dev/guide/output#option-1-json): TiptapTransformer.fromYdoc()
2023-06-03 00:26:24 +08:00
"ydocState"?: {}, // optionally contains the entire yDoc as base64. Contact us to enable this property!
"clientsCount": 100 // number of currently connected clients
}
```
2023-08-05 07:45:55 +08:00
### Signing
All requests to your webhook URL will contain a header called `X-Hocuspocus-Signature-256` that signs the entire message with your secret. You can find it in the [settings](https://collab.tiptap.dev/apps/settings) of your Tiptap Collab app.
2023-06-15 04:01:37 +08:00
2023-08-05 07:45:55 +08:00
## Management API
2023-06-15 04:01:37 +08:00
2023-08-05 07:45:55 +08:00
In addition to the websocket protocol, each Tiptap Collab app comes with a REST API for managing your documents. It's exposed directly from your Tiptap Collab app, so it's available at your custom URL:
2023-06-15 04:01:37 +08:00
`https://YOUR_APP_ID.collab.tiptap.cloud/`
2023-08-05 07:45:55 +08:00
Authentication is done using an API secret which you can find in the [settings](https://collab.tiptap.dev/) of your Tiptap Collab app. The secret must be sent as an `Authorization` header.
2023-06-15 04:01:37 +08:00
2023-08-05 07:45:55 +08:00
If your document identifier contains a slash (`/`), just make sure to encode it as `%2F`, e.g. using `encodeURIComponent` of vanilla JavaScript.
2023-06-15 04:01:37 +08:00
2023-08-05 07:45:55 +08:00
### Create Document
```bash
POST /api/documents/:identifier
```
This call takes a binary Yjs update message (an existing Yjs document on your side must be encoded using `Y.encodeStateAsUpdate`) and creates a document. This can be used to seed documents before a user connects to the Tiptap Collab server.
This endpoint will return the HTTP status `204` if the document was created successfully, or `409` if the document already exists. If you want to overwrite it, you must delete it first.
2023-06-15 04:01:37 +08:00
```bash
curl --location 'https://YOUR_APP_ID.collab.tiptap.cloud/api/documents/DOCUMENT_NAME' \
--header 'Authorization: YOUR_SECRET_FROM_SETTINGS_AREA' \
--data '@yjsUpdate.binary.txt'
```
2023-08-05 07:45:55 +08:00
### Get Document
2023-06-15 04:01:37 +08:00
2023-08-05 07:45:55 +08:00
```bash
GET /api/documents/:identifier?format=:format&fragment=:fragment
```
This call exports the given document with all fragments in JSON format. We export either the current in-memory version or the version read from the database. If the document is currently open on your server, we will return the in-memory version.
2023-06-15 04:01:37 +08:00
2023-08-05 07:45:55 +08:00
`format` supports either `yjs` or `json`. Default: `json`
2023-06-15 04:01:37 +08:00
2023-08-05 07:45:55 +08:00
If you choose the `yjs` format, you'll get the binary Yjs update message created with `Y.encodeStateAsUpdate`.
2023-06-15 04:01:37 +08:00
2023-08-05 07:45:55 +08:00
`fragment` can be an array (`fragment=a&fragment=b`) of or a single fragment that you want to export. By default we'll export all fragments. Note that this is only taken into account when using the `json` format, otherwise you'll always get the whole Yjs document.
2023-06-15 04:01:37 +08:00
```bash
curl --location 'https://YOUR_APP_ID.collab.tiptap.cloud/api/documents/DOCUMENT_NAME' \
--header 'Authorization: YOUR_SECRET_FROM_SETTINGS_AREA'
2023-08-05 07:45:55 +08:00
```
### Delete Document
2023-06-15 04:01:37 +08:00
2023-08-05 07:45:55 +08:00
```bash
DELETE /api/documents/:identifier
2023-06-15 04:01:37 +08:00
```
2023-08-05 07:45:55 +08:00
This endpoint deletes a document from the server after closing any open connection to the document.
2023-06-15 04:01:37 +08:00
2023-08-05 07:45:55 +08:00
It returns either HTTP status `204` if the document was deleted successfully or `404` if the document was not found.
2023-06-15 04:01:37 +08:00
```bash
curl --location --request DELETE 'https://YOUR_APP_ID.collab.tiptap.cloud/api/documents/DOCUMENT_NAME' \
--header 'Authorization: YOUR_SECRET_FROM_SETTINGS_AREA'
```
2023-08-05 07:45:55 +08:00
## Screenshots
2023-06-15 04:01:37 +08:00
2023-08-05 07:45:55 +08:00
Here are some screenshots of Tiptap Collab to give you an idea what of Tiptap Collab looks like.
2023-06-15 04:01:37 +08:00
2023-08-05 07:45:55 +08:00
### Dashboard
2023-06-15 04:01:37 +08:00
2023-08-05 07:45:55 +08:00
View key metrics such as total or concurrent connections of your Tiptap Collab app.
[![Cloud Dashboard](https://tiptap.dev/images/docs/server/cloud/dashboard.png)](https://tiptap.dev/images/docs/server/cloud/dashboard.png)
### Documents
Get insight into all your documents, such as size or delete them if you want.
2023-06-03 00:26:24 +08:00
[![Cloud Documents](https://tiptap.dev/images/docs/server/cloud/documents.png)](https://tiptap.dev/images/docs/server/cloud/documents.png)
2023-08-05 07:45:55 +08:00
### Logging
View real-time log events for information about currently loaded or modified documents.
[![Cloud Settings](https://tiptap.dev/images/docs/server/cloud/logging.png)](https://tiptap.dev/images/docs/server/cloud/logging.png)
### Settings
Manage the authentication of your application or defined webhooks in the settings.
2023-06-03 00:26:24 +08:00
[![Cloud Settings](https://tiptap.dev/images/docs/server/cloud/settings.png)](https://tiptap.dev/images/docs/server/cloud/settings.png)
2023-06-15 04:01:37 +08:00
## Need anything else?
2023-06-03 00:26:24 +08:00
Contact us on [Discord](https://tiptap.dev/discord) or send an email to [humans@tiptap.dev](mailto:humans@tiptap.dev).