Skip to content

lordShandilya/ProDocs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

19 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ“ Real-Time Collaborative Document Editor

A WebSocket-based collaborative document editing platform that enables multiple users to simultaneously edit documents with instant synchronization across all connected clients.

Node.js Express.js Socket.io JavaScript

โœจ Features

  • 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

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   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โ”‚
                    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿš€ Getting Started

Prerequisites

  • Node.js (v14 or higher)
  • npm or yarn

Installation

  1. Clone the repository
git clone https://github.com/yourusername/collaborative-doc-editor.git
cd collaborative-doc-editor
  1. Install dependencies
npm install
  1. Create a .env file
PORT=3000
NODE_ENV=development
  1. Start the server
npm start

The server will start on http://localhost:3000

๐Ÿ“š API Documentation

REST Endpoints

Create Document

POST /docs/create
Content-Type: application/json

{
  "title": "My Document",
  "content": "Initial content"
}

Response: 200 OK
{
  "msg": "New resource created."
}

Get Document by ID

GET /docs/:id

Response: 200 OK
{
  "title": "My Document",
  "content": "Document content here..."
}

Error: 404 Not Found
{
  "error": "File not found"
}

List All Documents

GET /docs/all

Response: 200 OK
{
  "files": [
    { "id": "abc123", "title": "My Document" },
    { "id": "xyz789", "title": "Another Doc" }
  ]
}

WebSocket Events

Client โ†’ Server

Join Document Room

socket.emit('join-document', documentId);

Send Document Update

socket.emit('document-update', newContent);

Server โ†’ Client

Receive Document Update

socket.on('document-update', (content) => {
  // Update editor with new content
});

User Joined Notification

socket.on('message', (msg) => {
  // "User {socketId} has joined!"
});

๐Ÿ› ๏ธ Technical Implementation

Storage Strategy

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 Map for O(1) document lookups
  • Reconstructed on server startup by scanning metadata files
  • Gracefully handles corrupted files without breaking initialization

Debounced File Writes

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" once

Force Save on Disconnect:

  • Ensures pending changes are saved when users leave
  • Prevents data loss on unexpected disconnections

WebSocket Room Management

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);

๐Ÿ“ Project Structure

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

๐Ÿงช Testing

Manual Testing with Web Client

  1. Start the server
  2. Open the provided HTML client in multiple browser tabs
  3. Create or join a document using the same document ID
  4. Type in one tab and observe real-time updates in others

Testing Persistence

  1. Create a document and add content
  2. Wait 2+ seconds for debounced save
  3. Stop the server (Ctrl+C)
  4. Restart the server
  5. Retrieve the document - content should persist

๐Ÿ”ง Configuration

Environment Variables

Variable Description Default
PORT Server port 3000
NODE_ENV Environment mode development

Adjustable Parameters

Debounce Delay (in StorageManagement.utils.js):

const WRITE_DELAY = 2000; // milliseconds

๐Ÿšง Known Limitations

  • 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

๐Ÿ”ฎ Future Enhancements

  • 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

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the project
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

๐Ÿ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ‘ค Author

Your Name

๐Ÿ™ Acknowledgments

  • 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!

About

Real time collaboration platform for documents.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors