How bytemaster-1/chatty works
Project type and purpose
• This is a full-stack, real-time chat application built with the MERN stack (MongoDB, Express, React, Node.js), enhanced with Socket.io for real-time messaging, JWT for auth, TailwindCSS/DaisyUI for styling, Zustand for client state, and Cloudinary for media storage.
High-level components
a. Frontend (React + Vite)
b. Backend API & Socket.io server (Node.js + Express)
c. Database (MongoDB)
d. External services: Cloudinary for image uploads
e. Client-side global state (Zustand)
f. Authentication (JWT)
Relationships & data flows
• The React UI issues HTTP requests (login, signup, fetch chats) to the Express REST API.
• On successful auth, the backend returns a JWT. The client stores it (e.g., in localStorage) and includes it in subsequent HTTP headers.
• For real-time messaging, the client opens a WebSocket connection (Socket.io) to the same Node.js server, passing the JWT for verification.
• The Socket.io server authenticates the token, maintains an online-users list, and rebroadcasts messages to target clients.
• Express controllers handle REST endpoints for auth (/auth), user profile (/users), and message CRUD (/messages).
• Database layer: models (User, Message) interact with MongoDB through a db.js helper.
• Cloudinary library handles media upload in message.controller.
• Seed scripts can prepopulate the database with test users/messages.
Architectural patterns & principles
• Layered architecture:
• Presentation Layer (React components & pages)
• API Layer (Express routes → controllers → models)
• Data Layer (MongoDB via Mongoose-like abstractions in models)
• Event-driven real-time layer (Socket.io) for push-based updates.
• JWT-based stateless authentication.
• Separation of concerns: controllers, routes, middlewares.
• Modular design: libs (db, cloudinary, socket, utils) extracted into reusable modules.
• Global state via Zustand to simplify passing data between components.
Capturing essential details
• Differentiate authentication flow vs. messaging flow.
• Error handling paths if desired (e.g., 401 on invalid token).