ShareJS - Concurrent Document Editing
ShareJS is a server and client library designed for real-time, concurrent document editing using operational transformation (OT). It supports OT on plain-text and arbitrary JSON data, allowing multiple users to collaborate on content simultaneously. The server component runs on Node.js, while the client library functions in both Node.js and web browsers, aiming for broad browser compatibility. It depends on LiveDB for its database backend and data model, and explicitly requires developers to provide a communication transport layer that guarantees in-order message delivery, warning against common pitfalls like using Socket.IO without careful configuration. The current stable version mentioned is 0.7.40. Key differentiators include its transport-agnostic design and its focus on raw OT primitives, leaving UI implementation to the developer. The project appears to be unmaintained as of early 2026, with no recent activity on its GitHub repository since around 2017.
Common errors
-
Error: Cannot find module 'share'
cause The 'share' package has not been installed or is not resolvable in the current environment.fixRun `npm install share` in your project directory to install the package. -
Error: Cannot find module 'livedb'
cause The 'livedb' package, a mandatory dependency for ShareJS, has not been installed.fixRun `npm install livedb` in your project directory. Depending on your chosen storage, you might also need `livedb-mongo` or similar. -
TypeError: require(...).server.createClient is not a function
cause Attempting to call `createClient` directly on the `share` module or `share.server` without properly requiring the `share` module, or using incorrect import syntax.fixEnsure you are using `var share = require('share');` and then `share.server.createClient(...)`. Double-check the exact casing and property access. -
Data synchronization errors or corrupted documents with Socket.IO
cause Socket.IO, by default, does not guarantee in-order message delivery, which is a strict requirement for ShareJS's operational transformation to function correctly.fixMigrate to a transport like `node-browserchannel` or ensure your WebSocket implementation explicitly enforces in-order message delivery. Avoid relying on default Socket.IO behavior for ShareJS.
Warnings
- breaking The ShareJS project, including its core repository (share/ShareJS), appears to be abandoned. The last significant commit was around 2017. Users should be aware that there will likely be no further bug fixes, security patches, or feature development. Consider alternative, actively maintained operational transformation libraries.
- gotcha ShareJS requires a communication transport that guarantees in-order message delivery. Using libraries like `socket.io` directly without specific configuration can lead to messages arriving out of order, causing data corruption or synchronization issues.
- gotcha The server-side API method to create a ShareJS instance is named `createClient`, which can be confusing as it creates a 'server' object that acts as a client to the LiveDB backend. This naming choice is explicit in the documentation but often leads to initial confusion.
- gotcha Documentation for the REST API and middleware (`share.use`) is partially external or incomplete within the main ShareJS repository. Users may need to consult separate repositories (`share/rest`, `share/middleware`) for full details, which might also suffer from the overall lack of maintenance.
Install
-
npm install share -
yarn add share -
pnpm add share
Imports
- livedb
import livedb from 'livedb';
var livedb = require('livedb'); - share
import share from 'share';
var share = require('share'); - createClient
var shareServer = require('share').createClient({backend: backend});var shareServer = require('share').server.createClient({backend: backend});
Quickstart
const livedb = require('livedb');
const share = require('share');
const http = require('http');
const express = require('express');
const WebSocket = require('ws');
// 1. Initialize LiveDB backend (in-memory for simple example)
const backend = livedb.client(livedb.memory());
// 2. Create ShareJS server instance
const shareServer = share.server.createClient({backend: backend});
// 3. Set up HTTP server for serving client-side code (optional, but common)
const app = express();
app.use(express.static(__dirname + '/public')); // Serve static files
const httpServer = http.createServer(app);
const wss = new WebSocket.Server({ server: httpServer });
// 4. Handle WebSocket connections for ShareJS clients
wss.on('connection', (ws) => {
const stream = WebSocket.createWebSocketStream(ws);
shareServer.listen(stream);
console.log('New ShareJS client connected via WebSocket.');
});
const PORT = process.env.PORT || 8000;
httpServer.listen(PORT, () => {
console.log(`ShareJS server listening on http://localhost:${PORT}`);
console.log('You will need a client-side implementation to connect.');
});