Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

CollabDocs β€” Real-Time Collaborative Document Editor


A production-ready Google Docs-style collaborative document editor built with React, Node.js, MongoDB, and Socket.io. Live at : https://collab-doc-rose.vercel.app/

✨ Features

  • Real-Time Collaboration β€” Multiple users editing simultaneously with live sync
  • Rich Text Editor β€” TipTap-powered with bold, italic, headings, lists, code blocks, links, and more
  • Live Presence β€” See who's in the document with colored user indicators
  • Auto Save β€” Documents auto-save every 5 seconds via WebSocket
  • Version History β€” Full history with restore capability
  • Comments System β€” Inline comments with replies and resolve/unresolve
  • Document Sharing β€” Invite collaborators by email with viewer/editor roles
  • Dark Mode β€” Full dark/light theme support
  • JWT Auth β€” Secure signup/login with bcrypt password hashing
  • Protected Routes β€” Frontend and backend route protection

πŸ—οΈ Tech Stack

Frontend

Tech Purpose
React 18 + Vite UI framework
TailwindCSS Styling
Zustand Client state management
TanStack React Query Server state + caching
TipTap Rich text editor
Socket.io Client Real-time events
React Router v6 Routing
React Hot Toast Notifications
Lucide React Icons

Backend

Tech Purpose
Node.js + Express HTTP server
MongoDB + Mongoose Database
Socket.io WebSocket server
JWT Authentication tokens
bcryptjs Password hashing
helmet + cors Security middleware
express-rate-limit Rate limiting
express-validator Input validation
dompurify + jsdom HTML sanitization

πŸ“ Folder Structure

collab-docs/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ config/
β”‚   β”‚   └── database.js          # MongoDB connection
β”‚   β”œβ”€β”€ controllers/
β”‚   β”‚   β”œβ”€β”€ authController.js    # signup, login, getMe
β”‚   β”‚   β”œβ”€β”€ documentController.js # CRUD + share
β”‚   β”‚   β”œβ”€β”€ commentController.js  # comments + replies
β”‚   β”‚   β”œβ”€β”€ versionController.js  # version history
β”‚   β”‚   └── userController.js     # user search
β”‚   β”œβ”€β”€ middleware/
β”‚   β”‚   β”œβ”€β”€ auth.js              # JWT middleware
β”‚   β”‚   β”œβ”€β”€ errorHandler.js      # Global error handler
β”‚   β”‚   └── validate.js          # express-validator helper
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ User.js
β”‚   β”‚   β”œβ”€β”€ Document.js
β”‚   β”‚   β”œβ”€β”€ Comment.js
β”‚   β”‚   └── Version.js
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   β”œβ”€β”€ auth.js
β”‚   β”‚   β”œβ”€β”€ documents.js
β”‚   β”‚   β”œβ”€β”€ comments.js
β”‚   β”‚   β”œβ”€β”€ versions.js
β”‚   β”‚   └── users.js
β”‚   β”œβ”€β”€ sockets/
β”‚   β”‚   └── socketHandlers.js    # All Socket.io logic
β”‚   β”œβ”€β”€ utils/
β”‚   β”‚   └── sanitize.js          # DOMPurify HTML sanitizer
β”‚   β”œβ”€β”€ .env.example
β”‚   β”œβ”€β”€ package.json
β”‚   └── server.js                # Entry point
β”‚
└── frontend/
    β”œβ”€β”€ src/
    β”‚   β”œβ”€β”€ components/
    β”‚   β”‚   β”œβ”€β”€ comments/
    β”‚   β”‚   β”‚   └── CommentsPanel.jsx
    β”‚   β”‚   β”œβ”€β”€ cursors/
    β”‚   β”‚   β”‚   └── ActiveUsers.jsx
    β”‚   β”‚   β”œβ”€β”€ editor/
    β”‚   β”‚   β”‚   β”œβ”€β”€ CollaborativeEditor.jsx
    β”‚   β”‚   β”‚   β”œβ”€β”€ SaveStatus.jsx
    β”‚   β”‚   β”‚   β”œβ”€β”€ ShareModal.jsx
    β”‚   β”‚   β”‚   └── VersionHistory.jsx
    β”‚   β”‚   β”œβ”€β”€ toolbar/
    β”‚   β”‚   β”‚   └── Toolbar.jsx
    β”‚   β”‚   └── ui/
    β”‚   β”‚       β”œβ”€β”€ Avatar.jsx
    β”‚   β”‚       β”œβ”€β”€ Modal.jsx
    β”‚   β”‚       β”œβ”€β”€ PageLoader.jsx
    β”‚   β”‚       β”œβ”€β”€ ProtectedRoute.jsx
    β”‚   β”‚       └── Skeleton.jsx
    β”‚   β”œβ”€β”€ hooks/
    β”‚   β”‚   β”œβ”€β”€ useAutoSave.js
    β”‚   β”‚   β”œβ”€β”€ useComments.js
    β”‚   β”‚   β”œβ”€β”€ useDocument.js
    β”‚   β”‚   └── useSocket.js
    β”‚   β”œβ”€β”€ pages/
    β”‚   β”‚   β”œβ”€β”€ Dashboard/index.jsx
    β”‚   β”‚   β”œβ”€β”€ EditorPage/index.jsx
    β”‚   β”‚   β”œβ”€β”€ Login/index.jsx
    β”‚   β”‚   └── Signup/index.jsx
    β”‚   β”œβ”€β”€ services/
    β”‚   β”‚   β”œβ”€β”€ api.js            # Axios instance + all API calls
    β”‚   β”‚   └── socket.js         # Socket.io client
    β”‚   β”œβ”€β”€ store/
    β”‚   β”‚   β”œβ”€β”€ authStore.js      # Zustand auth state
    β”‚   β”‚   β”œβ”€β”€ editorStore.js    # Zustand editor state
    β”‚   β”‚   └── themeStore.js     # Zustand theme state
    β”‚   β”œβ”€β”€ utils/
    β”‚   β”‚   └── debounce.js
    β”‚   β”œβ”€β”€ App.jsx
    β”‚   β”œβ”€β”€ index.css
    β”‚   └── main.jsx
    β”œβ”€β”€ index.html
    β”œβ”€β”€ package.json
    β”œβ”€β”€ tailwind.config.js
    └── vite.config.js

πŸš€ Getting Started

Prerequisites

  • Node.js >= 18.x
  • MongoDB (local or MongoDB Atlas)
  • npm or yarn

1. Clone the repository

git clone <repo-url>
cd collab-docs

2. Backend Setup

cd backend

# Install dependencies
npm install

# Set up environment variables
cp .env.example .env
# Edit .env with your values (see Environment Variables section)

# Start development server
npm run dev

3. Frontend Setup

cd frontend

# Install dependencies
npm install

# Set up environment variables
cp .env.example .env
# Edit .env with your values

# Start development server
npm run dev

4. Open the app


πŸ” Environment Variables

Backend (backend/.env)

NODE_ENV=development
PORT=5000

# MongoDB
MONGODB_URI=mongodb://localhost:27017/collab-docs
# Or MongoDB Atlas:
# MONGODB_URI=mongodb+srv://<user>:<password>@cluster.mongodb.net/collab-docs

# JWT (use a strong random string in production)
JWT_SECRET=your_super_secret_jwt_key_change_this_in_production_min_32_chars
JWT_EXPIRES_IN=7d

# Frontend URL (for CORS)
CLIENT_URL=http://localhost:5173

# Rate Limiting
RATE_LIMIT_WINDOW_MS=900000
RATE_LIMIT_MAX=100

Frontend (frontend/.env)

VITE_API_URL=http://localhost:5000/api
VITE_SOCKET_URL=http://localhost:5000

πŸ“‘ API Reference

Auth

Method Endpoint Description Auth Required
POST /api/auth/signup Register new user ❌
POST /api/auth/login Login user ❌
GET /api/auth/me Get current user βœ…
PUT /api/auth/profile Update profile βœ…

Documents

Method Endpoint Description Auth Required
GET /api/documents List documents βœ…
POST /api/documents Create document βœ…
GET /api/documents/:id Get document βœ…
PUT /api/documents/:id Update document βœ…
DELETE /api/documents/:id Delete document βœ…
POST /api/documents/:id/share Share document βœ…
DELETE /api/documents/:id/collaborators/:userId Remove collaborator βœ…

Comments

Method Endpoint Description
GET /api/comments/:documentId Get comments
POST /api/comments Create comment
POST /api/comments/:id/replies Add reply
PUT /api/comments/:id/resolve Toggle resolve
DELETE /api/comments/:id Delete comment

Versions

Method Endpoint Description
GET /api/versions/:documentId Get version history
POST /api/versions/:documentId/restore/:versionId Restore version

πŸ”Œ Socket.io Events

Client β†’ Server

Event Payload Description
JOIN_DOCUMENT { documentId } Join document room
LEAVE_DOCUMENT { documentId } Leave document room
DOCUMENT_CHANGE { documentId, content } Broadcast content change
CURSOR_POSITION { documentId, position, selection } Broadcast cursor
TITLE_CHANGE { documentId, title } Broadcast title change
SAVE_DOCUMENT { documentId, content, title } Trigger server save

Server β†’ Client

Event Payload Description
USER_JOINED { user, documentId } User joined notification
USER_LEFT { socketId, userId, name } User left notification
ACTIVE_USERS { users } Current users in room
DOCUMENT_CHANGE { content, userId, timestamp } Content from other user
CURSOR_POSITION { userId, socketId, name, color, position } Cursor from other user
TITLE_CHANGE { title, userId } Title from other user
DOCUMENT_SAVED { documentId, savedAt } Save confirmation
SAVE_ERROR { message } Save error

πŸ›‘οΈ Security

  • Password hashing: bcryptjs with 12 salt rounds
  • JWT tokens: Expire in 7 days, verified on every request
  • HTML sanitization: DOMPurify on all user-generated content
  • Rate limiting: 100 requests per 15 minutes per IP
  • Helmet.js: HTTP security headers
  • CORS: Configured for specific frontend origin
  • Input validation: express-validator on all endpoints
  • Socket auth: JWT verified on WebSocket connection

🚒 Production Deployment

Backend

  1. Set NODE_ENV=production
  2. Use a strong JWT_SECRET
  3. Use MongoDB Atlas or a managed MongoDB service
  4. Consider Redis for Socket.io scaling across multiple instances
  5. Use PM2 for process management: pm2 start server.js

Frontend

cd frontend
npm run build
# Serve the dist/ folder with nginx or any static file server

Docker (optional)

Consider containerizing both services with Docker Compose for easier deployment.


πŸ§ͺ Development Tips

  • The backend uses nodemon for hot reload
  • The frontend uses Vite's HMR for instant updates
  • Socket.io requests are proxied through Vite in development (see vite.config.js)
  • Use MongoDB Compass for visual database inspection
  • Use the browser's DevTools Network tab to monitor WebSocket frames

πŸ“„ License

MIT

About

A production-ready Google Docs-style collaborative document editor built with React, Node.js, MongoDB, and Socket.io.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages