A WebSocket-based collaborative document editing platform that enables multiple users to simultaneously edit documents with instant synchronization across all connected clients.
- Real-Time Collaboration: Multiple users can edit the same document simultaneously with instant updates
- Document Management: Create, retrieve, and list documents via RESTful API
- Persistent Storage: Documents are saved to disk with metadata tracking
- Automatic Recovery: Server rebuilds document index from filesystem on restart
- Optimized Writes: Debounced file saves reduce disk I/O while maintaining data integrity
- Room-Based Architecture: WebSocket rooms ensure updates only reach relevant collaborators
- CORS Enabled: Cross-origin support for web-based clients
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ Client 1 โ โ Client 2 โ โ Client N โ
โ (Browser) โ โ (Browser) โ โ (Browser) โ
โโโโโโโโฌโโโโโโโ โโโโโโโโฌโโโโโโโ โโโโโโโโฌโโโโโโโ
โ โ โ
โ WebSocket โ WebSocket โ WebSocket
โ Connection โ Connection โ Connection
โ โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโผโโโโโโโโโโโ
โ Socket.io Server โ
โ (Document Rooms) โ
โโโโโโโโโโโโฌโโโโโโโโโโโ
โ
โโโโโโโโโโโโผโโโโโโโโโโโ
โ Express REST API โ
โ /docs endpoints โ
โโโโโโโโโโโโฌโโโโโโโโโโโ
โ
โโโโโโโโโโโโผโโโโโโโโโโโ
โ Storage Manager โ
โ (Debounced Writes) โ
โโโโโโโโโโโโฌโโโโโโโโโโโ
โ
โโโโโโโโโโโโผโโโโโโโโโโโ
โ File System โ
โ *.txt + *.meta.jsonโ
โโโโโโโโโโโโโโโโโโโโโโโ
- Node.js (v14 or higher)
- npm or yarn
- Clone the repository
git clone https://github.com/yourusername/collaborative-doc-editor.git
cd collaborative-doc-editor- Install dependencies
npm install- Create a
.envfile
PORT=3000
NODE_ENV=development- Start the server
npm startThe server will start on http://localhost:3000
POST /docs/create
Content-Type: application/json
{
"title": "My Document",
"content": "Initial content"
}
Response: 200 OK
{
"msg": "New resource created."
}GET /docs/:id
Response: 200 OK
{
"title": "My Document",
"content": "Document content here..."
}
Error: 404 Not Found
{
"error": "File not found"
}GET /docs/all
Response: 200 OK
{
"files": [
{ "id": "abc123", "title": "My Document" },
{ "id": "xyz789", "title": "Another Doc" }
]
}Join Document Room
socket.emit('join-document', documentId);Send Document Update
socket.emit('document-update', newContent);Receive Document Update
socket.on('document-update', (content) => {
// Update editor with new content
});User Joined Notification
socket.on('message', (msg) => {
// "User {socketId} has joined!"
});Documents are stored using a dual-file approach:
storage/
โโโ abc123.txt # Document content
โโโ abc123_meta.json # { "title": "..." }
โโโ xyz789.txt
โโโ xyz789_meta.json
In-Memory Index:
- Uses JavaScript
Mapfor O(1) document lookups - Reconstructed on server startup by scanning metadata files
- Gracefully handles corrupted files without breaking initialization
To optimize disk I/O, writes are debounced with a 2-second delay:
User types: H โ timer starts (2s)
User types: e โ timer resets (2s)
User types: l โ timer resets (2s)
User types: l โ timer resets (2s)
User types: o โ timer resets (2s)
User stops โ 2s later: save "Hello" onceForce Save on Disconnect:
- Ensures pending changes are saved when users leave
- Prevents data loss on unexpected disconnections
Socket.io's built-in room feature isolates document updates:
// User joins document room
socket.join(documentId);
// Broadcast to room (excluding sender)
socket.to(documentId).emit('document-update', content);collaborative-doc-editor/
โโโ index.js # Server entry point
โโโ routes/
โ โโโ docs.route.js # REST API routes
โโโ handlers/
โ โโโ StorageManagement.handler.js # WebSocket initialization
โโโ utils/
โ โโโ StorageManagement.utils.js # File operations & persistence
โโโ storage/ # Document storage directory
โโโ .env # Environment variables
โโโ .gitignore
โโโ package.json
- Start the server
- Open the provided HTML client in multiple browser tabs
- Create or join a document using the same document ID
- Type in one tab and observe real-time updates in others
- Create a document and add content
- Wait 2+ seconds for debounced save
- Stop the server (Ctrl+C)
- Restart the server
- Retrieve the document - content should persist
| Variable | Description | Default |
|---|---|---|
PORT |
Server port | 3000 |
NODE_ENV |
Environment mode | development |
Debounce Delay (in StorageManagement.utils.js):
const WRITE_DELAY = 2000; // milliseconds- No Authentication: Anyone can access and edit any document
- File-Based Storage: Not suitable for high-scale deployments
- Simple Conflict Resolution: Last-write-wins (no operational transforms)
- No Version History: Previous document states are not tracked
- Ephemeral on Free Hosting: Files are lost on Render free tier restarts
- User authentication with JWT
- Document ownership and permissions
- Database integration (PostgreSQL/MongoDB)
- Operational Transforms or CRDTs for better conflict resolution
- Version history and rollback functionality
- Rich text editing support
- Presence indicators (show active users)
- Document sharing via links
- Export documents (PDF, Markdown, etc.)
- Search functionality across documents
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the project
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Your Name
- GitHub: @lordShandilya
- LinkedIn: Vishal Shandilya
- Socket.io for WebSocket implementation
- Express.js for REST API framework
- The Node.js community for excellent documentation
โญ If you found this project helpful, please consider giving it a star!