This guide explains how to add multiplayer support to applications using React.js and the Lexical editor.
Basic Integration
Begin by incorporating lexical-provider into your Lexical project:
The following code snippet demonstrates how to achieve a basic integration between a Lexical editor component and Codox:
codoxAPI
Codox exposes its multiplayer capabilities to Lexical through a standard Lexical React plugin component. The CodoxCollabPlugin manages the merging and synchronization of content as local and remote changes occur. Its primary role within the application is to control the start and stop of collaboration sessions.
The plugin provides an API layer accessible through a forwardRef. This ref should be declared on the container component and passed as a prop. To interact with the API, use ref.current to access the instance.
Initializing Document
Before initiating or joining a Codox session, the latest version of the document content must be available. This content, typically formatted as a JSON object adhering to Lexical’s document structure (including a root attribute that encapsulates the document), can be provided directly as a prop or fetched from a remote repository.
In the sample code above, we showed how you might accomplish this if the
document was fetched dynamically from a remote repository.
We passed the fetched content a utility function validateStateStructure
from the provider module which will perform basic validation of the
document state against the known nodes. It will throw on any
invalid structure or unrecognized nodes are found in the input document state.
Registering Nodes
The Codox plugin enhances node capabilities by adding metadata essential for data merging and reconciliation tasks. It is important that the plugin is informed of all nodes you intend to use with Lexical, including both official implementations under the @lexical namespace and any custom nodes you may develop for your application.
To facilitate this, utilize the registerNodesWithCodox() function provided by the Codox provider. This function accepts an array of node classes and returns them wrapped with the necessary listeners and metadata. These enhanced nodes can then be incorporated into the Lexical initialization configuration as shown below:
Start and Stop Session
In line with React’s lifecycle conventions, initiate and terminate Codox sessions using side-effects managed through a forward ref prop. This allows for precise control over session management based on the application state:
Updating Lexical State
In a React-based implementation of a single-player Lexical instance, a custom plugin component is typically used to subscribe to updates from the Lexical editor state. This component would typically handle an onChange event from the parent component to update the local state using setEditorState. Here’s an example implementation:
The native onChange emitter in Lexical does not differentiate between changes made by the local user and those made remotely during collaboration. To address this, the Codox plugin provides a contentChanged hook that triggers each time there is a change in the editor’s content, with an indication whether the changes are local or remote:
Undo/Redo support
Codox supports undo and redo operations, leveraging the built-in undo/redo functionality of the Lexical editor. This is implemented through the LexicalHistoryPlugin from the official package @lexical/react/LexicalHistoryPlugin. To ensure stability and compatibility with codox sync’s merging process, initialize the HistoryPlugin alongside the Codox plugin within your editor configuration. It’s recommended to use this built-in plugin rather than custom undo/redo implementations to avoid potential sync conflicts.
Error handling and rollback
Error from codox
For a comprehensive list of potential errors emitted by Codox, refer to the error events documentation. To handle these errors, subscribe to the relevant events and implement appropriate responses:
Error from Lexical
The Codox plugin is equipped to handle certain errors that may arise from Lexical operations, helping to prevent these errors from corrupting the document. The standard error handling strategy involves rolling back the document to its state prior to the error detection. If an onError callback is provided by the client’s application, Codox will invoke this callback and then perform a rollback to safeguard the document integrity: