Real-Time Collaboration in Joplin
The base technologies used for real-time collaboration:
-
WebSockets
In websockets server acts as a mediator for the communication between the clients. where changes by a client is sent to the server and the server emits/updates other clients in the session/room about the changes.
This is very much server-dependent.
-
WebRTC
In WebRTC only job of the server is to connect a new client to other clients in a session/room. While clients communicate with each other directly through a P2P connection.
This has very less load on the server.
We could directly build on these base technologies, but that requires a lot of research, development, testing(Just to implement the communication/conflict resolution) and this is also very much error-prone.
Building on these base technologies (which are available as regular javascript APIs in all major browsers), there are libraries that provide easy integration, development, and additional features for implementing several kinds of real-time collaborative applications.
So, What are the libraries which we can use to create a Real-Time Collaborative Editor for a Joplin note?
Below are the options and pros/cons of the libraries that we can use:
Socket.IO
Socket.IO was the first technology that I looked into for developing a real-time application. This library is built on top of WebSockets. This is more suitable for chat applications / similar applications with new data coming in rather than the same data edited by multiple users.
Pros:
- Easy to implement
- Reliable and very active community.
- Full duplex communication over an HTTP connection(meaning reliable transmission of data).
Cons:
- We have to implement all the conflict resolution techniques.
- Needs a server in between. server-dependent communication between clients.
GUN
Gun is also implemented using WebSockets and provides conflict resolution using CRDTs. It is a very developer-friendly library (looking into the efforts they put into documenting and facilitating developers who are new to GUN). It needs a signaling server for P2P communication. the organization is currently working on establishing a direct connection between peers without the need of a signaling server through this project: AXE
Pros:
- P2P communication.(less load on the server)
- Provides user authentication (need to explore more to better understand how it works)
- Very active community and extremely visual tutorials for developers new to the technology.
Cons:
- Need to implement the data updations from and to the editor we use.(i.e. no support for editor libraries provided by the community).
- Signaling server is needed.
ShareDB (ShareJS)
ShareDB is also implemented using WebSockets and provides conflict resolution using OT. it is an open-source real-time backend. It basically maintains a copy of the data on the server-side and lets clients push changes to it. these changes are then synchronized using OT and all other clients are updated of it.
Pros:
- Offline support(meaning when the client is offline the changes made are stored at the client end and updated to the server on connection).
- Extensible via middleware with community-provided modules for authorization, etc.
Cons:
- Needs a server between clients for communication.
- Supporting other data types requires writing transforms from scratch, which is extremely time-intensive and error-prone (but supports basic data types like JSON, plain text, and rich-text)
TogetherJS
TogetherJS is just a plugin kind of thing that enables users on the website to collaborate. It needs a button to be added to the UI of the web app and when a user clicks it... it provides the user with a URL that is shared with other users. they can open that URL and collaborate with the same website. It is more of a Surfing-the-website-collaboratively than a library which we can use to edit a note collaboratively.
YJS (Y-WebRTC)
Y-WebRTC is implemented using WebRTC and provides conflict resolution using CRDTs.
Pros:
- Support for decentralized, peer-to-peer shared data editing
- Fairly rich set of supported types: Maps/arrays, text, and rich text (Quill only)
- Out of the box support for a variety of common UI libraries (Ace, Quill, CodeMirror, etc)
- Offline support
Cons:
- Needs a signaling server to start the P2P communication.
Conclusion:
- No matter what technology we use, we need a server... but the thing we need to choose here is should we use a WebSocket-based library or a WebRTC based one.
- As these servers are no different from an ExpressJS app... So yes, these can be dockerized.
- All the above-mentioned repositories are very active.
- Basic implementation into Joplin:
- A signaling server.
- A Collaborative web app (which loads the note from Joplin Server API).