How jamesnpe13/comms-app works
Project Type and Purpose
• This is a full-stack, real-time messaging web application (“Comms-App”) built with a React SPA frontend and an Express/MySQL backend. It provides user registration, authentication (JWT), RESTful APIs for user and message management, and WebSocket-based real-time chat via Socket.IO.
Main Components
a. Frontend (client/)
• React SPA using React Router for navigation
• Context providers for Auth, Messaging, and Socket
• axiosInstance for REST calls to the backend
• Socket.IO client for real-time message delivery
• UI components (layout, panes, tiles, modals) and SCSS styling
b. Backend (server/)
• Express.js server handling REST endpoints (auth, users, messaging)
• Socket.IO server integrated into Express for bidirectional events
• Middleware chain for JWT authentication, role authorization, password hashing and error handling
• Controllers for business logic (authController, messagingController, usersController)
• Database layer with MySQL connection, migrations, and normalized tables (users, chat_rooms, messages, memberships)
c. Database
• MySQL instance (hosted locally or in managed service)
• Tables: users, chat_rooms, messages, room_memberships
• Indexed for efficient querying by timestamp and room
d. External Services & Deployment
• Vercel (or CDN) hosting static React build
• Railway (or similar PaaS) hosting Node/Express app
• Load balancer / API gateway (for horizontal scaling of stateless backend)
• Environment variables for secrets, database credentials, CORS origin
Interactions and Data Flow
• User opens React SPA → React Router loads pages (Login, Registration, Dashboard).
• On login/registration → axiosInstance sends REST call → Express authController → MySQL → returns JWTs.
• React stores tokens (browserStorage utility) and initializes Socket.IO connection with access token header.
• Real-time messaging:
• Client emits “join_room”, “send_message” events to Socket.IO server.
• Server validates token → emits message to room namespace → broadcast to subscribers.
• Clients listen for “receive_message” events to update UI.
• Protected API routes use authenticateToken and authorizeUser middleware.
Architectural Patterns & Design Principles
• Client-Server separation (SPA + REST + WebSockets)
• Layered architecture: Presentation (React), Application (Express controllers, middleware), Data (MySQL)
• Stateless backend (JWT allows horizontal scaling)
• Pub/Sub messaging via Socket.IO rooms
• Modular code organization (controllers, routes, middleware, database migrations)
Capturing Essential Architectural Elements
• Authentication flow (login → token issuance → token storage → token refresh)
• Real-time messaging flow (join room, send/receive message)
• Data persistence (MySQL read/write)
• How future clients (e.g., mobile or desktop) could plug into the same REST + Socket.IO layer.
• Scalability: stateless servers behind a load balancer, CDN for frontend.