Skip to content

Commit

Permalink
Merge pull request #26 from hpi-schul-cloud/move-tldraw-docu
Browse files Browse the repository at this point in the history
Move tldraw documentation
  • Loading branch information
CeEv authored May 24, 2024
2 parents c7d861d + 084d73a commit 519255c
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 0 deletions.
81 changes: 81 additions & 0 deletions docs/services/tldraw/How it works.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# How it works

## Configuration

- NEST_LOG_LEVEL - logging level
- FEATURE_TLDRAW_ENABLED - flag determining if tldraw is enabled
- TLDRAW_URI - address of tldraw server
- INCOMING_REQUEST_TIMEOUT - request timeout
- TLDRAW_DB_URL - mongoDB connection string
- TLDRAW__SOCKET_PORT - port number for websockets connection
- TLDRAW__PING_TIMEOUT - timeout for ping-pong during establishing websockets connection
- TLDRAW__FINALIZE_DELAY - delay in ms before checking if can finalize a tldraw board
- TLDRAW__GC_ENABLED - if tldraw garbage collector should be enabled
- TLDRAW__DB_COMPRESS_THRESHOLD - threshold size for tldraw mongo documents compression
- TLDRAW__MAX_DOCUMENT_SIZE - max size of a single tldraw document in mongo
- TLDRAW__ASSETS_ENABLED - enables uploading assets to tldraw board
- TLDRAW__ASSETS_SYNC_ENABLED - enables synch of tldraw board assets with filestorage (no longer used)
- TLDRAW__ASSETS_MAX_SIZE - maximum asset size in bytes
- TLDRAW__ASSETS_ALLOWED_MIME_TYPES_LIST - listy of allowed assets MIME types
- REDIS_URI - Redis connection string
- TLDRAW_CLIENT_REPLICAS - number of pods for tldraw-client
- TLDRAW_SERVER_REPLICAS - number of pods for tldraw-server
- TLDRAW_ADMIN_API_CLIENT__API_KEY - authorization API key for accessing tldraw controller (delete flow)
- TLDRAW_ADMIN_API_CLIENT__BASE_URL - address of tldraw controller (delete flow)

In order to have deletion functionality fully working you have to fill those feature flags, e.g.:
- ADMIN_API__ALLOWED_API_KEYS=["7ccd4e11-c6f6-48b0-81eb-abcdef123456"]
- TLDRAW_ADMIN_API_CLIENT__API_KEY="7ccd4e11-c6f6-48b0-81eb-abcdef123456"
- TLDRAW_ADMIN_API_CLIENT__BASE_URL="http://localhost:3349"

## Create
![Create tldraw workflow](./assets/Create TLDRAW.drawio.svg)

Creation of Tldraw starts with creation proccess for Courses and CourseBoard. It has Representation in CourseBoard as card's element (BoardNode in db). After creating Representation of drawing we can enter actual tldraw SPA client (left side of picture).

1. User enters CourseBoard and creates Representation of whiteboard (tldraw) in CourseCard.
2. Data is saved and feedback with proper creation is given - user can see Representation and can enter whiteboard.
3. By entering whiteboard user is redirected to SPA tldraw-client.
4. Tldraw-client is starting WS connection with tldraw-server.
5. Tldraw-server firstly checks if user has permission to this resource (by checking if user has a permission to Representation of whiteboard -BoardNode).
Id of Representation is same as drawingName, which is visible in tldraw-client url.
6. If user has permission tldraw-server is allowing to remain connection and getting drawing data from separate tldraw-db. If there were no drawing data saved tldraw-server will create it automatically.

## Usage
![Usage tldraw workflow](./assets/Use tldraw.drawio.svg)

### Connection

1. user joins tldraw board
2. tldraw-client connects to one of the tldraw-server pods and tries to establish websocket connection
3. tldraw-server calls schulcloud-server via HTTP requests to check user permissions, if everything is fine the websocket connection is established
4. tldraw-server gets stored tldraw board data from mongodb and sends it via websocket to connected users
5. tldraw-server starts subscribing to Redis PUBSUB channel corresponding to tldraw board name to listen to changes from other pods

### Sending updates/storing data

1. tldraw-client sends user's drawing changes to the tldraw-server via websocket connection
2. tldraw-server stores the board update in the mongodb - basically creates a diff between what's already stored and what's being updated
3. tldraw-server pushes the update to correct Redis channel so that clients connected to different pods have synchronized board data
4. other pods subscribing to Redis channel send updates to their connected clients via websocket whenever they see a new message on Redis channel

## Delete
![Delete tldraw workflow](./assets/Delete TLDRAW.drawio.svg)

1. User from schulcloud app in CourseBoard deletes whiteboard (tldraw) instance form CardBoard.
2. Having drawingName sc-server is removing Representation data in sc-database - BoardNodes collection ( drawingName === BoardNode id)
3. Sc-server is calling tldraw-server via tldraw-management rules in tldraw-server-svc to delete all data that has given id).
4. After deletion user sees refreshed state of CourseBoard.

## Assets
### files upload

Images/GIFs can be uploaded into tldraw whiteboard by every user with access to the board. We use s3 storage to physically store uploaded assets while tldraw only holds URL to a resource.

The files are uploaded by calling schulcloud-api's fileController upload endpoint. This is possible because tldraw is represented as a boardnode called drawing-element. Mongo id of this drawing-element is a roomId used in URL param when connecting to a specific board.

### files deletion

Because of the undo/redo functionality of tldraw (user can basically undo an upload of an image, undo a deletion, then redo upload etc.) we needed a way to clean up unused assets from the storage. We could not use soft delete/restore endpoints every time undo/redo happens due to various issues with performance/user experience and technical challenges that arose when testing different scenarios. We decided to go with cron job solution: once per day, at midnight by default, we would go through each board stored in database, get every asset that's stored as URL but no longer used as an active drawing and then delete all of them via amqp call to filesStorage service.

For implementation details, take a look at: tldraw-files.console.ts.
18 changes: 18 additions & 0 deletions docs/services/tldraw/Local setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Local setup

### To run tldraw locally:

1. Run all of the apps needed for schulcloud like mongodb, backend, frontend, file storage etc.
2. Run redis i.e. in a docker container, it will work on localhost:6379 by default which is what the REDIS_URI env var is set to
3. On schulcloud-server repo: npm run nest:start:tldraw:dev
4. On tldraw-client repo: npm run dev

### Create new whiteboard:

1. Go to a course
2. Go to 'Column board'
3. Create a new card and a new 'Whiteboard' element within it, then click it
4. A new browser tab with URL like: http://localhost:4000/tldraw?roomName=65c37329b2f97cc714d31c00 will open
5. Change the port part from 4000 to 3046, which is the default port of tldraw-client app
6. You should see a working tldraw whiteboard now

90 changes: 90 additions & 0 deletions docs/services/tldraw/Technical details.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Technical details

## Backend
We are using pure Web Sockets implementation to establish connection between client and server. The reason why we used pure websockets is because tldraw-client is using y-websockets from Yjs library, that does not connect with socket.io web sockets. We also have to implement broadcasting mechanism to provide stateless solution. To achive that goal we decided to use Redis. We used ioredis library to connect to our Redis instance. Everytime user make some changes at first it is handled to the server instance that he is connected to an then this change is send to the channel with the name of the board and servers that also operate that board are listening on this channel so they can recive messages from other servers and provide those changes to users that are connected to this pod. We added the same mechanism for awareness channel so every user from every pod can see other users cursours.


Tldraw is deployed as a separate application from schoulcloud-server working on the same namespace as schoulcloud-server. On the backend side we have added couple new resources:

- tldraw-deployment - deployment for tldraw-server's instances.
- tldraw-server-svc - service for tldraw-service to communicate with tldraw-client (WS) and schoulcloud-server (management e.g. deletion of data)
- tldraw-svc-monitor - service to collect metrics from tldraw. Apart from typical metrics like request time we also added two application-level metrics:
- sc_tldraw_users - number of active users on boards
- sc_tldraw_boards - number of active boards
- tldraw-ingress - for steering web external traffic to tldraw-server (for now management rules in tldraw-server-svc are closed from external clients)


### Tldraw-server code structure

- tldraw.ws.service.ts - main service responsible for establishing web socket connection as well as saving data to database. Responsibe for Redis communication.
- tldraw.controller.ts - controller that expose HTTP deletion method outside the tldraw-server application.
- tldraw.server.ts - service used by TldrawController.
- y-mongodb.ts - main adapter to connect with mongodb, provides transaction mechanism, calucalate diffs between revision and to apply updates.
- tldraw-board.repo.ts - repository object to connect TldrawWsService and YMongodb.
- tldraw.repo.ts - repository used by TldrawService to find and delete boards from database.
- ws-shared-doc.do.ts - main structure representing tldraw drawing during web socket communication. it holds all the web-socket addresses that are connected to this board, so we can inform all the connected clients about changes.
- tldraw-drawing.entity.ts - object representing tldraw drawingn entity in database.
- metrics.service.ts - service resonsible for storing application-level metrics.


On the backend side we are also using Yjs library to store tldraw board in memory and to calculate diffs after the board is changed.

## Frontend

### Key Files
- stores/setup.ts – this file provides a real-time collaboration environment for a drawing application using the WebSocket and Yjs libraries.
- hooks/useMultiplayerState.ts – custom hook for managing multiplayer state.
- App.tsx – main application component integrating Tldraw and multiplayer state.

### Frontend Technologies

The frontend of the project is built using React and leverages various libraries and tools for enhanced functionality. Here is an overview of the key frontend technologies:

- React: A JavaScript library for building user interfaces.
- Yjs: A real-time collaboration framework for synchronizing shared state.
- Tldraw: A library for drawing functionalities in the application. We use the old version of tldraw: https://github.com/tldraw/tldraw-v1, after the tldraw team releases the official update of the new version, we will work on the new version and integrate it with the needs of our users.

### State Managment

1. Yjs is integrated into the project for real-time collaboration. The central state (shapes, bindings, assets) is managed using Yjs maps.
2. store.ts handles the configuration of Yjs, WebSocket connections, and provides centralized maps for shapes, bindings, and assets
3. useMultiplayerState.ts -This hook manages the multiplayer state, including loading rooms, handling file system operations, and updating Yjs maps:
- Mounting and handling changes in Tldraw App.
- Presence management and user updates.
- File system operations like opening and saving projects.

#### useTldrawUiSanitizer.ts
This hook is designed to observe changes in the DOM, specifically targeting certain buttons and a horizontal rule (< hr>), and hides them if they match a specific ID pattern. We hide this elements and left just only Language and Keyboard shortcuts.

#### Event Handling
- onMount: Handles mounting of the Tldraw app.
- onChangePage: Manages page changes and updates Yjs maps.
- onUndo and onRedo: Handle undo and redo operations.
- onChangePresence: Manages presence changes in the collaborative environment.
- onAssetCreate: This function is triggered when a user attempts to upload an asset (like an image or a file).

#### Useful links
- https://tldraw.dev/ - documentation for the new version of tldraw

- https://old.tldraw.com/ - tldraw live application

- https://github.com/tldraw/tldraw-v1 - tldraw v1 repo

- https://github.com/MaxNoetzold/y-mongodb-provider - code from this package was used to add mongodb as a persistence to tldraw

- https://discord.com/invite/SBBEVCA4PG discord channel with open questions and answers

- https://grafana.dbildungscloud.dev/d/b6b28b2b-3129-4772-8102-e32981d2c2e3/devops-tldraw-metrics?orgId=1&refresh=1m&var-source=sc-dev-dbc&var-env=main&var-env=tldraw-debugging - grafana v

- https://grafana.dbildungscloud.org/d/b6b28b2b-3129-4772-8102-e32981d2c2e0/devops-tldraw?orgId=1&from=now-6h&to=now&refresh=1m - grafana metrics

- https://github.com/nimeshnayaju/yjs-tldraw - yjs with tldraw POC

- https://github.com/yjs/y-websocket/tree/master/bin - yjs/y-websocket repo

- https://github.com/erdtool/yjs-scalable-ws-backend/tree/main - Yjs scalable WS backend with redis example

- https://teamchat.dbildungscloud.de/channel/G9hJWv92zXEESKK3X - rocketchat discussion "tldraw syncronisation for release again"

- https://teamchat.dbildungscloud.de/group/SagK4sCyujhu6yZr8 - rocketchat discussion "Tldraw deployment"s

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions docs/services/tldraw/assets/Create TLDRAW.drawio.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions docs/services/tldraw/assets/Delete TLDRAW.drawio.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/services/tldraw/assets/Use tldraw.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions docs/services/tldraw/assets/Use tldraw.drawio.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 519255c

Please sign in to comment.