How vazanth/realtime-chat-app works
1. This repository is split into two cooperating applications: a TypeScript/Node.js backend and a React/Vite frontend. The backend exposes REST endpoints and a Socket.IO server; the frontend is a single-page chat UI that talks to both.
2. The backend entry points are src/server.ts and src/app.ts. app.ts wires Express middleware, routing, and response/error handling, while server.ts starts the HTTP server and attaches the socket layer.
3. The HTTP API is organized by domain:
- authController.ts for sign-up/sign-in
- userController.ts for user-related retrieval
- chatController.ts for creating chats, fetching chat history, and creating groups
- messageController.ts for fetching/sending chat messages and file uploads
4. Request validation is a first-class boundary. Each domain has a Zod schema in src/schema, and src/middleware/validator.ts enforces those schemas before controller logic runs. Validation errors are normalized through helper utilities such as parseSchemaError.ts.
5. The backend appears intentionally lightweight in persistence: src/data plus helpers/fileOperations.ts suggest file-based storage rather than a database. The README confirms the project expects local JSON files like users.json and chat.json, which means the architecture is centered on filesystem-backed state.
6. Real-time chat is handled separately from the REST API in src/socket. The backend socket module likely manages room membership and message broadcast for chat sessions, while the frontend keeps a live connection through frontend/src/context/SocketContext.tsx.
7. File sharing is a distinct path through the system. The backend has a Multer middleware, a public directory for stored assets, and message endpoints for sending files. That makes uploads a separate transport concern from normal message text.
8. The frontend is a route-based SPA. Routes.tsx and pages/ define the login, register, and chat screens, while App.tsx acts as the top-level composition layer.
9. The chat screen is built from reusable UI parts in frontend/src/components, especially ChatList, ChatBox, Header, Modal, and Popover. These components split the UI into navigation, conversation selection, message display/composition, and auxiliary dialogs.
10. Data flow on the frontend is straightforward: a user authenticates through the login/register pages, then lands on the chat page, which fetches chat metadata and message history over HTTP and uses Socket.IO for live updates. The socket context centralizes connection state so the UI can react to incoming messages globally.
11. There is a clear boundary between persistent chat state and transient live state. REST endpoints are used for setup and retrieval, while the socket layer handles in-session delivery and notifications.
12. The project is designed to run locally with Node 18+, TypeScript on both sides, and Vite/Tailwind on the frontend. There is no sign of an external database or third-party messaging service; the architecture is mostly self-contained around Express, Socket.IO, local storage, and a browser SPA.
Main entry points: Chat.tsx Chat page · controllers Controllers · Routes.tsx Routes · index.ts Socket
Components 20
Backend
Express REST API, validation, filesystem-backed state, and Socket.IO realtime delivery.
- Server · Starts the Node process, binds HTTP, and attaches realtime sockets.
backend/src/server.ts - App · Configures middleware, routing, and response/error handling.
backend/src/app.ts - Routes · Domain route entrypoints for chat, messages, and users.
backend/src/routes - Controllers · Auth, chat, message, and user request handlers.
backend/src/controllers - Validator · Runs Zod schemas before controller logic and normalizes validation failures.
backend/src/middleware/validator.ts - Schemas · Domain validation schemas for chat, message, and user requests.
backend/src/schema - File state · Local JSON-backed persistence and helper operations for app state.
backend/src/data - Uploads · File upload transport and public asset handling for message attachments.
backend/src/middleware/multer.ts - Socket · Realtime messaging layer for rooms, joins, and broadcast updates.
backend/src/socket/index.ts - Chat socket · Chat-specific realtime coordination and message fanout.
backend/src/socket/chat.ts
Frontend
React/Vite single-page chat UI with route screens, reusable components, and socket context.
- App · Top-level React composition for the client application.
frontend/src/App.tsx - Routes · Route map for login, register, and chat screens.
frontend/src/Routes.tsx - Socket context · Centralized Socket.IO connection and incoming message state for the UI.
frontend/src/context/SocketContext.tsx - Login · Sign-in entry screen.
frontend/src/pages/Login.tsx - Register · Sign-up entry screen.
frontend/src/pages/Register.tsx - Chat page · Main authenticated chat workspace.
frontend/src/pages/Chat.tsx - Chat list · Conversation and contact navigation.
frontend/src/components/ChatList.tsx - Chat box · Message timeline and composition area.
frontend/src/components/ChatBox.tsx - Header · Top-level conversation and app controls.
frontend/src/components/Header.tsx - Overlays · Modal, popover, and animated UI helpers.
frontend/src/components
Connections 22
- Server → App: boots · The server starts the Express app and HTTP listener.
- Server → Socket: attaches · The server also initializes realtime connections.
- App → Routes: mounts · App middleware delegates requests into domain routes.
- Routes → Controllers: dispatches · Routes forward work to domain controllers.
- Controllers → Validator: validates · Requests are checked before controller logic runs.
- Validator → Schemas: uses · Validation is driven by Zod schemas.
- Controllers → File state: reads/writes · Domain handlers persist and retrieve local app state.
- Controllers → Uploads: uploads · Message/file endpoints pass through upload handling.
- Socket → Chat socket: delegates · Socket bootstrap hands chat behavior to chat-specific logic.
- App → Routes: composes · The app shell renders the routed screens.
- Routes → Login: shows · Login screen is one route target.
- Routes → Register: shows · Register screen is one route target.
- Routes → Chat page: shows · Chat screen is the authenticated route target.
- Chat page → Chat list: uses · Conversation navigation is built into the chat screen.
- Chat page → Chat box: uses · Message timeline and composer are built into the chat screen.
- Chat page → Header: uses · Header controls frame the active chat view.
- Chat page → Overlays: uses · Auxiliary dialogs and popovers support chat interactions.
- Socket context → Socket: connects · The client keeps a live Socket.IO connection to the backend.
- Login → Controllers: auth HTTP · Sign-in submits to backend authentication handling.
- Register → Controllers: auth HTTP · Sign-up submits to backend authentication handling.
- Chat page → Controllers: chat HTTP · Chat metadata and message history are fetched over REST.
- Chat page → Uploads: file upload · Attachment flows use the backend upload path.