{"library":"sharedb","title":"ShareDB: Realtime JSON OT Database Backend","description":"ShareDB is a powerful realtime database backend built on Operational Transformation (OT) for JSON documents, enabling concurrent multi-user collaboration and synchronous editing with eventual consistency. It is the core realtime backend for the DerbyJS web application framework. Currently in version 5.2.2, ShareDB maintains a consistent release cadence with frequent patch and minor updates addressing bugs and performance improvements. Key features include realtime query subscriptions, easy integration with various databases (like MongoDB via `sharedb-mongo`), horizontal scalability through pub/sub, document projections, middleware support for access control, and offline change syncing. It supports both server-side and browser environments and provides access to historic document versions and realtime user presence syncing. It differentiates itself by offering a complete OT-based solution for collaborative data management.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install sharedb"],"cli":null},"imports":["import { Backend } from 'sharedb';","import { MemoryDB } from 'sharedb';","import { MemoryPubSub } from 'sharedb';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { Backend, MemoryDB, MemoryPubSub } from 'sharedb';\nimport WebSocket from 'ws';\nimport http from 'http';\n\nconst backend = new Backend({\n  db: new MemoryDB(),\n  pubsub: new MemoryPubSub()\n});\n\nconst server = http.createServer((req, res) => {\n  res.writeHead(200, { 'Content-Type': 'text/plain' });\n  res.end('ShareDB Server\\n');\n});\n\nconst wss = new WebSocket.Server({ server: server });\n\nwss.on('connection', (ws) => {\n  const stream = new WebSocket.WebSocketJSONStream(ws);\n  backend.listen(stream);\n});\n\nserver.listen(8080, () => {\n  console.log('ShareDB server listening on http://localhost:8080');\n  // Example: Create a new document via a connection\n  const connection = backend.connect();\n  const doc = connection.get('myCollection', 'myDocument');\n\n  doc.fetch((err) => {\n    if (err) throw err;\n    if (doc.type === null) {\n      doc.create({ message: 'Hello, ShareDB!' }, 'json0', () => {\n        console.log('Document created:', doc.data);\n      });\n    } else {\n      console.log('Document already exists:', doc.data);\n    }\n  });\n});\n\n// To run this, you'll need 'ws' and 'sharedb' installed.\n// For the client-side stream, you might need 'rich-websocket-jsonstream' or similar.","lang":"typescript","description":"This quickstart demonstrates setting up a basic ShareDB server with in-memory storage and pub/sub, integrated with a WebSocket server. It initializes the backend, connects a WebSocket server, and shows how to create a document using a ShareDB connection.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}