How zero-squared/open-chats works
Project type and purpose
• This is a full-stack, real-time chat platform (open-chats) implemented in Node.js/Express.
• It combines server-rendered UI (EJS templates under views), a RESTful API (controllers/api), static assets (public), and a WebSocket layer for live chats (sockets/index.js).
File structure
• Top-level components:
• app.js – entry point, configures Express, middleware, routes, and socket server.
• controllers – handles HTTP logic (main pages, admin, API).
• routes – maps URLs to controllers (mainRoutes.js, apiRoutes.js).
• models – data layer (chat, message, user, label, role, index.js for ORM/Mongoose).
• views & public – frontend: EJS templates + CSS/JS assets.
• sockets – real-time event handlers (socket.io integration).
• middleware & utils – session management, uploads, auth guards, config, ImageKit integration.
• setup – initial seed and configuration.
• Architectural choice: classic MVC augmented with a WebSocket “micro-layer” for live communication.
Key insights from README
• Requirements: Node v22, pnpm, pm2.
• Two modes: development (pnpm run dev) and production (pm2).
• .env files drive environment-specific config.
Main components
a. Frontend (Client Browser)
• EJS-rendered pages (home, login, register, profile, chats, admin).
• Static JS modules handle UI logic (public/js) and establish socket.io connections.
b. Backend (Express Server)
• HTTP layer:
• Routes → Controllers (mainController, chatViewController, adminController).
• API layer: REST endpoints (auth, chat, message, user, label, localization).
• WebSocket layer: sockets/index.js for real-time chat events.
• Middleware: session management, authentication checks, file uploads (multerUpload), locals injection.
• Utilities: config loader, session helpers, ImageKit API wrapper, message templates.
c. Database
• Models: user, role, chat, message, label.
• ORM or ODM (configured in models/index.js).
• Session store (if using a DB-backed session).
d. External services
• ImageKit (for avatar/storage).
• (Optionally) Email or push services if extended.
e. Deployment
• pm2 process manager.
• Environment variables (.env.dev, .env.prod).
Relationships & interactions
• Browser ↔ HTTP(S) Express
• GET pages, POST forms, AJAX/API fetches.
• Browser ↔ WebSocket Express
• Real-time chat events: send message, receive broadcast.
• Express → Database
• CRUD via models for users, chats, messages, labels.
• Express → External API (ImageKit)
• Upload and fetch images.
• Express (session middleware) ↔ session store (could be in-memory or DB).
Architectural patterns & design principles
• MVC for server-rendered views.
• Layered services: routing, controller, service/util, data access.
• Event-driven micro-layer for WebSocket.
• Separation of concerns: admin vs. user controllers, API vs. view controllers.
• Config-driven by environment variables.
Detailed elements
• app.js as the central orchestrator.
• middleware chain (session → user auth → locals).
• controllers split: view vs. API vs. admin.
• models cluster around the DB.
• static asset pipeline (public folder).
• setup/seed process as a side note.