Skip to content

yasinhajilou/ctrl-w

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ctrl+w - Real-Time Device Sync Platform

🚧 Project Status: Active Development (60% Complete)
A production-grade, real-time web application for instant text and image sharing between devices using temporary pairing codes. Built with modern engineering patterns and best practices for technical interviews.

Node.js Express MongoDB Socket.io JWT License


πŸ“Œ Project Overview

ctrl+w enables seamless, secure real-time synchronization between devices (phones, laptops, tablets) using simple 6-digit pairing codes. No app installation or account creation required for basic useβ€”just enter a code and start syncing.

🎯 Key Features (Implemented)

βœ… Dual Authentication System

  • Optional JWT-based auth with access + refresh token pattern
  • Anonymous session support for privacy-focused users
  • Role-based access control (user/admin)
  • Secure password hashing with bcrypt (10 salt rounds)

βœ… Database Architecture

  • MongoDB Atlas cloud database with Mongoose ODM
  • Four core models: User, Session, Message, File
  • Optimized compound indexes for query performance
  • TTL indexes for automatic session cleanup

βœ… Security & Performance

  • Rate limiting on authentication endpoints (5 attempts/15min)
  • Helmet.js for security headers
  • CORS configuration with credential support
  • Token rotation on refresh for enhanced security
  • httpOnly cookies for refresh token storage

βœ… Developer Experience

  • Comprehensive ESLint + Prettier configuration
  • Jest unit and integration test suites
  • Environment-based configuration
  • Modular service-layer architecture

🚧 In Progress

⏳ Real-Time Communication (Week 3, Starting Soon)

  • WebSocket server with Socket.io
  • Room-based message broadcasting
  • Presence detection and typing indicators

⏳ File Upload System (Week 4)

  • Cloudinary CDN integration
  • 4MB file size limit with validation
  • Image optimization and thumbnails

⏳ Frontend Application (Week 5)

  • Responsive UI (mobile + desktop)
  • Vanilla JavaScript (no framework dependencies)
  • WebSocket client for real-time updates

⏳ Production Deployment (Week 6)

  • CI/CD pipeline with GitHub Actions
  • Vercel deployment configuration
  • Environment management

πŸ—οΈ Technical Architecture

System Design

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    CLIENT LAYER                         β”‚
β”‚  β€’ Vanilla JS Frontend                                  β”‚
β”‚  β€’ WebSocket Client (Socket.io)                         β”‚
β”‚  β€’ Responsive CSS (Mobile-first)                        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                  β”‚
                  β”‚ HTTP/WebSocket
                  β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   APPLICATION LAYER                      β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚ Auth Service β”‚  β”‚Session Serviceβ”‚ β”‚ File Service  β”‚ β”‚
β”‚  β”‚              β”‚  β”‚               β”‚  β”‚               β”‚ β”‚
β”‚  β”‚ β€’ JWT Gen    β”‚  β”‚ β€’ Code Gen    β”‚  β”‚ β€’ Cloudinary  β”‚ β”‚
β”‚  β”‚ β€’ Verify     β”‚  β”‚ β€’ Validation  β”‚  β”‚ β€’ Validation  β”‚ β”‚
β”‚  β”‚ β€’ Refresh    β”‚  β”‚ β€’ Expiry      β”‚  β”‚ β€’ Metadata    β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚                                                          β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚              MIDDLEWARE LAYER                      β”‚ β”‚
β”‚  β”‚  β€’ Auth (JWT verification)                         β”‚ β”‚
β”‚  β”‚  β€’ Rate Limiting (express-rate-limit)              β”‚ β”‚
β”‚  β”‚  β€’ Validation (express-validator)                  β”‚ β”‚
β”‚  β”‚  β€’ Error Handling (centralized)                    β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                  β”‚
                  β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    DATA LAYER                           β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”           β”‚
β”‚  β”‚  MongoDB Atlas   β”‚  β”‚   Cloudinary     β”‚           β”‚
β”‚  β”‚                  β”‚  β”‚                  β”‚           β”‚
β”‚  β”‚ β€’ Users          β”‚  β”‚ β€’ Image Storage  β”‚           β”‚
β”‚  β”‚ β€’ Sessions       β”‚  β”‚ β€’ CDN Delivery   β”‚           β”‚
β”‚  β”‚ β€’ Messages       β”‚  β”‚ β€’ Optimization   β”‚           β”‚
β”‚  β”‚ β€’ Files          β”‚  β”‚                  β”‚           β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Database Schema (MongoDB)

// User Model
{
  email: String (unique, indexed),
  password: String (bcrypt hashed),
  role: Enum ['user', 'admin'],
  refreshToken: String,
  createdAt: Date,
  updatedAt: Date
}

// Session Model
{
  pairingCode: String (6-digit, unique, indexed),
  creator: ObjectId (ref: User, optional),
  participants: [{ socketId, deviceInfo, joinedAt }],
  status: Enum ['active', 'expired', 'closed'],
  expiresAt: Date (TTL indexed),
  messageCount: Number,
  fileCount: Number
}

// Message Model
{
  session: ObjectId (ref: Session, indexed),
  senderSocketId: String,
  sender: ObjectId (ref: User, optional),
  content: String (max 5000 chars),
  type: Enum ['text', 'system'],
  createdAt: Date
}

// File Model
{
  session: ObjectId (ref: Session, indexed),
  uploaderSocketId: String,
  cloudinaryUrl: String,
  cloudinaryPublicId: String,
  originalName: String,
  fileType: String,
  fileSize: Number (max 4MB)
}

πŸš€ Getting Started

Prerequisites

  • Node.js >= 18.0.0
  • npm >= 9.0.0
  • MongoDB Atlas account (free tier)
  • Git for version control

Installation

  1. Clone the repository
git clone https://github.com/yourusername/ctrl-w.git
cd ctrl-w
  1. Install dependencies
npm install
  1. Set up environment variables
cp .env.example .env

Edit .env with your credentials:

# MongoDB Atlas connection string
MONGO_URI=mongodb+srv://username:password@cluster.mongodb.net/ctrlw

# JWT secrets (generate with: node -e "console.log(require('crypto').randomBytes(64).toString('hex'))")
JWT_ACCESS_SECRET=your_generated_secret_here
JWT_REFRESH_SECRET=your_generated_secret_here

# Server configuration
PORT=5000
NODE_ENV=development
  1. Start the development server
npm run dev

Server will start on http://localhost:5000


πŸ“‘ API Documentation

Authentication Endpoints

Register User

POST /api/auth/register
Content-Type: application/json

{
  "email": "user@example.com",
  "password": "password123"
}

Response (201):

{
  "success": true,
  "message": "User registered successfully",
  "data": {
    "user": {
      "id": "65abc...",
      "email": "user@example.com",
      "role": "user",
      "createdAt": "2024-01-15T10:00:00.000Z"
    },
    "accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
  }
}

Login

POST /api/auth/login
Content-Type: application/json

{
  "email": "user@example.com",
  "password": "password123"
}

Refresh Token

POST /api/auth/refresh
Content-Type: application/json

{
  "refreshToken": "your_refresh_token"
}

Get Current User

GET /api/auth/me
Authorization: Bearer <access_token>

Logout

POST /api/auth/logout
Authorization: Bearer <access_token>

πŸ§ͺ Testing

Run All Tests

npm test

Run Tests with Coverage

npm test -- --coverage

Run Tests in Watch Mode

npm run test:watch

Current Test Coverage

--------------------|---------|----------|---------|---------|
File                | % Stmts | % Branch | % Funcs | % Lines |
--------------------|---------|----------|---------|---------|
All files           |   87.5  |   82.3   |   90.1  |   88.2  |
 models/            |   92.1  |   88.7   |   95.3  |   93.4  |
  User.js           |   95.2  |   91.2   |   97.1  |   96.3  |
  Session.js        |   91.8  |   87.5   |   94.2  |   92.7  |
  Message.js        |   89.3  |   85.1   |   92.8  |   90.1  |
  File.js           |   92.4  |   90.8   |   96.5  |   94.2  |
 services/          |   85.7  |   78.9   |   88.3  |   86.5  |
  authService.js    |   85.7  |   78.9   |   88.3  |   86.5  |
 middleware/        |   83.2  |   76.4   |   85.1  |   84.3  |
  auth.js           |   83.2  |   76.4   |   85.1  |   84.3  |
--------------------|---------|----------|---------|---------|

πŸ› οΈ Tech Stack & Key Concepts

Backend Technologies

Technology Purpose Interview Relevance
Node.js Runtime environment Non-blocking I/O, event loop
Express.js Web framework Middleware pattern, routing
MongoDB + Mongoose Database NoSQL, schema design, indexing
JWT Authentication Stateless auth, token lifecycle
bcrypt Password hashing Salt rounds, adaptive hashing
Socket.io Real-time communication WebSocket, rooms, events
Cloudinary File storage CDN, cloud services integration

Design Patterns Implemented

  • Service Layer Pattern: Separation of business logic from HTTP layer
  • Middleware Chain: Request processing pipeline
  • Repository Pattern: Data access abstraction via Mongoose
  • Factory Pattern: Token generation in auth service
  • Strategy Pattern: Multiple authentication strategies (JWT, anonymous)

Security Features

  • βœ… JWT with refresh token rotation
  • βœ… Password hashing with bcrypt (10 salt rounds)
  • βœ… Rate limiting (5 attempts/15min on auth endpoints)
  • βœ… CORS with credential support
  • βœ… Helmet.js security headers
  • βœ… Input validation with express-validator
  • βœ… httpOnly cookies for refresh tokens
  • βœ… Environment-based secrets management

πŸ“Š Project Progress

Completed (60%)

  • Week 1: Project setup, MongoDB Atlas, authentication system

    • npm initialization with comprehensive package.json
    • ESLint + Prettier configuration
    • MongoDB connection with pooling
    • Environment variable management
  • Week 2: Database models and schema design

    • User model with bcrypt hashing
    • Session model with unique code generation
    • Message model with session linking
    • File model with Cloudinary integration
    • Comprehensive test suite (Jest)
  • Week 3 (Partial): Authentication & JWT

    • Auth service with JWT generation/verification
    • Access + refresh token implementation
    • Authentication middleware
    • Rate limiting middleware
    • Auth routes (register, login, refresh, logout)

In Progress (40%)

  • Week 3: WebSocket real-time layer
  • Week 4: File uploads with Cloudinary
  • Week 5: Frontend development
  • Week 6: Testing, CI/CD, deployment
  • Week 7: Documentation & polish

πŸ’Ό Interview Preparation Features

This project was built with technical interviews in mind. Key discussion points:

System Design

  • "How would you design a real-time messaging system?"
  • "How would you handle authentication at scale?"
  • "How would you implement rate limiting?"

Backend Architecture

  • Service layer pattern for business logic separation
  • Middleware chain for request processing
  • Stateless JWT authentication with refresh tokens

Database Design

  • Schema design: embedded vs referenced documents
  • Indexing strategy for query optimization
  • TTL indexes for automatic cleanup

Security

  • Token-based authentication
  • Password hashing with adaptive algorithms
  • Rate limiting for DDoS protection
  • CORS and security headers

Testing

  • Unit tests for model validation
  • Integration tests for API endpoints
  • Test coverage reporting

πŸ“ Project Structure

ctrl-w/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ config/
β”‚   β”‚   └── database.js              # MongoDB connection
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ User.js                  # User schema
β”‚   β”‚   β”œβ”€β”€ Session.js               # Session schema
β”‚   β”‚   β”œβ”€β”€ Message.js               # Message schema
β”‚   β”‚   β”œβ”€β”€ File.js                  # File schema
β”‚   β”‚   └── index.js                 # Model exports
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   └── authService.js           # Authentication logic
β”‚   β”œβ”€β”€ middleware/
β”‚   β”‚   β”œβ”€β”€ auth.js                  # JWT verification
β”‚   β”‚   └── rateLimiter.js           # Rate limiting
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   └── auth.routes.js           # Auth endpoints
β”‚   β”œβ”€β”€ tests/
β”‚   β”‚   β”œβ”€β”€ unit/
β”‚   β”‚   β”‚   └── models/              # Model unit tests
β”‚   β”‚   └── integration/
β”‚   β”‚       └── models/              # Model integration tests
β”‚   β”œβ”€β”€ app.js                       # Express configuration
β”‚   └── server.js                    # Server entry point
β”œβ”€β”€ .env.example                     # Environment template
β”œβ”€β”€ .eslintrc.json                   # ESLint configuration
β”œβ”€β”€ .prettierrc                      # Prettier configuration
β”œβ”€β”€ .gitignore                       # Git ignore rules
β”œβ”€β”€ jest.config.js                   # Jest configuration
β”œβ”€β”€ package.json                     # Dependencies
└── README.md                        # This file

πŸ”§ Scripts

# Development
npm run dev              # Start with nodemon (auto-reload)

# Production
npm start                # Start production server

# Testing
npm test                 # Run all tests
npm run test:watch       # Run tests in watch mode

# Code Quality
npm run lint             # Check code with ESLint
npm run lint:fix         # Fix ESLint issues
npm run format           # Format code with Prettier
npm run format:check     # Check formatting

🌟 Key Learning Outcomes

Technical Skills Demonstrated

Backend Development:

  • RESTful API design and implementation
  • Database schema design with MongoDB
  • JWT authentication with refresh tokens
  • WebSocket real-time communication
  • File upload handling and cloud storage
  • Rate limiting and security best practices

Software Engineering:

  • Service-oriented architecture
  • Middleware pattern implementation
  • Error handling strategies
  • Testing (unit + integration)
  • Environment configuration management

DevOps & Tools:

  • Git version control
  • npm package management
  • ESLint and Prettier for code quality
  • Jest for testing
  • MongoDB Atlas cloud database

πŸ“– Resources & References

Documentation

Learning Materials

  • Node.js Design Patterns (Book)
  • Clean Code by Robert C. Martin
  • System Design Interview by Alex Xu

🀝 Contributing

This is a learning project for interview preparation. Feedback and suggestions are welcome!

  1. Fork the repository
  2. Create a 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

πŸ‘€ Author

[Yasin Hajilou]


πŸ™ Acknowledgments

  • Built as part of technical interview preparation
  • Inspired by modern real-time collaboration tools
  • Thanks to the open-source community for excellent tools and libraries

⭐ Star this repo if you found it helpful for your interview prep!

🚧 Project actively under development - Check back for updates!

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors