This example showcases a multiplayer BlockNote rich text editor with YJS and Electric SQL. All collaborative data is synchronized through PostgreSQL, providing real-time editing with cursor awareness without requiring additional real-time infrastructure.
- Real-time Collaborative Editing: Multiple users can edit documents simultaneously
- Cursor Awareness: See other users' cursors and selections in real-time
- Rich Text Editor: Full-featured editor based on ProseMirror with BlockNote
- Offline Support: Works offline with automatic sync when reconnected
- CRDT-based: Conflict-free editing using YJS CRDT technology
- Database-backed: All data persisted in PostgreSQL with Electric SQL
This application demonstrates the powerful combination of:
- BlockNote: A modern, extensible rich text editor built on ProseMirror
- YJS: A CRDT library that enables real-time collaboration
- Electric SQL: A local-first database that syncs with PostgreSQL
- Y-Electric: A YJS connection provider that integrates YJS with Electric SQL
The integration works by:
- BlockNote uses YJS for collaborative editing via the
y-prosemirror
binding - Y-Electric handles the synchronization between YJS documents and PostgreSQL
- Electric SQL provides offline-first database capabilities with automatic sync
- All collaboration data flows through PostgreSQL, eliminating the need for WebSocket servers
- Node.js (v18 or higher)
- pnpm (v8 or higher)
- Docker and Docker Compose
-
Install dependencies:
pnpm install
-
Build the application:
pnpm build
-
Start the backend services using Docker Compose:
pnpm backend:up
Note: This stops and deletes volumes from any other running example backend containers to ensure a clean database state.
-
Start the development server:
pnpm dev:server
-
In a new terminal, start the client application:
pnpm dev:client
-
Open your browser and navigate to the application URL (typically
http://localhost:5173
) -
When you're done, stop the backend services:
pnpm backend:down
The editor is implemented in src/client/components/editor/EditorView.tsx
:
const editor = useCreateBlockNote({
collaboration: {
provider: {
awareness, // YJS awareness for cursor/selection sync
},
fragment: ydoc.getXmlFragment(`document-store`), // YJS fragment for document data
user: {
name: user.name,
color: user.color,
},
},
})
The YJS-Electric integration is handled in src/client/common/useElectric.ts
:
const provider = new ElectricProvider({
doc: ydoc,
documentUpdates: {
shape: { /* Electric SQL shape configuration */ },
sendUrl: /* API endpoint for updates */,
},
awarenessUpdates: {
shape: { /* Awareness sync configuration */ },
sendUrl: /* API endpoint for awareness updates */,
},
})
The PostgreSQL schema (db/migrations/01-create_yjs_tables.sql
) includes:
ydoc_update
: Stores YJS document updatesydoc_awareness
: Stores user awareness data (cursors, selections)files
: Document metadatausers
: User information
- Create a User: Select a user from the dropdown in the sidebar
- Edit Documents: Start typing in the editor - changes sync in real-time
- Collaborate: Open multiple browser tabs/windows with different users to see real-time collaboration
- Cursor Awareness: See other users' cursors and selections in real-time
- Offline Mode: Disconnect and continue editing - changes sync when reconnected
- Document Creation: Each document is represented as a YJS document with an XML fragment
- Real-time Sync: Y-Electric syncs YJS updates with PostgreSQL via Electric SQL
- Awareness: User cursors and selections are synced through the awareness protocol
- Conflict Resolution: YJS CRDT automatically resolves conflicts between concurrent edits
- Offline Support: Changes are stored locally and synced when connectivity is restored
This is an example application demonstrating the integration of BlockNote with Electric SQL. For issues or contributions related to:
- BlockNote: Visit the BlockNote repository
- Electric SQL: Visit the Electric SQL repository
- This Example: Open an issue
This project is licensed under the Apache 2.0 License.