ShareDB Collaboration
Real-time collaboration using ShareDB
Installation
yarn add "@rowsncolumns/sharedb sharedb reconnecting-websocket"npm install "@rowsncolumns/sharedb sharedb reconnecting-websocket"Quick Start
import { useShareDBSpreadsheet } from "@rowsncolumns/sharedb";
import ShareDBClient from "sharedb/lib/client";
import ReconnectingWebSocket from "reconnecting-websocket";
// Create ShareDB connection
const socket = new ReconnectingWebSocket("ws://localhost:8080");
const connection = new ShareDBClient.Connection(socket);
function SpreadsheetEditor() {
const [sheetData, setSheetData] = useState({});
const [sheets, setSheets] = useState([]);
const [tables, setTables] = useState([]);
const [sheetId, setSheetId] = useState(1);
const [activeCell, setActiveCell] = useState({ rowIndex: 1, columnIndex: 1 });
const { onBroadcastPatch, users, synced, isLeader } = useShareDBSpreadsheet({
connection,
collection: "spreadsheets",
documentId: "my-spreadsheet",
userId: "user-123",
title: "John Doe",
sheetId,
activeCell,
initialSheets: [],
onChangeSheetData: setSheetData,
onChangeSheets: setSheets,
onChangeTables: setTables,
onChangeActiveSheet: setSheetId,
calculateNow,
enqueueGraphOperation: (op) => {
// Handle dependency graph updates for formula recalculation
},
});
// Pass onBroadcastPatch to useSpreadsheetState
return <Spreadsheet sheetData={sheetData} sheets={sheets} users={users} />;
}Integration with useSpreadsheetState
Setting Up a ShareDB Server
Basic Server
Server with MongoDB Persistence
Server with PostgreSQL Persistence
Document Structure
Cell Key Format
Key
Description
CellDataV3 Structure
Presence Awareness
Leader Election
Error Handling
Comparison with Y.js
Feature
ShareDB
Y.js
Last updated