How sscoderin/chatrr works
Main Components
a. Frontend (Next.js)
• App‐Router pages under app/(pages)/chat for the chat UI
• Shared UI components under app/components (e.g., Header, SignIn/SignUp)
• Zustand stores (lib/store/useAuth.js, useChat.js) for client‐side state management
• Global layout (app/layout.tsx) and styling (globals.css)
b. Backend/API (Next.js API Routes)
• Auth routes (app/api/auth/*) for signup, signin, logout, update profile
• Message routes (app/api/message/*) for send, chats, contacts, chathistory
• pages/api/socket.js hosting the Socket.IO server for real‐time messaging
c. Middleware Layer
• arcjet.middleware.js for rate limiting/security
• auth.middleware.js and socket.auth.middleware.js to guard API & socket endpoints
d. Database
• MySQL instance (configured via lib/database_connect.js)
• Data models in models/MessageModel.js and models/UserModel.js
e. External Services
• Cloudinary for file hosting (images, avatars)
• Arcjet (ARCJET_KEY) for higher‐level security/rate‐limit
• JWT_SECRET used by auth middleware to sign/verify tokens
Relationships & Data Flows
• When a user visits the chat page, the Next.js frontend calls the auth API to validate or create a session
• Frontend uses useAuth store to hold user info, and useChat to manage real‐time message streams
• On successful sign‐in, the client opens a WebSocket (via Socket.IO) to pages/api/socket.js; socket.auth.middleware.js checks the JWT
• For each message sent:
– Frontend → Socket.IO channel → backend event handler in pages/api/socket.js
– Server persists to MySQL (via MessageModel)
– Server broadcasts to other clients in the same chat room
• For retrieving chat history or contact lists, the client hits RESTful endpoints in app/api/message/* (protected by auth.middleware.js)
• Arcjet middleware is applied globally to HTTP routes to throttle/secure API calls
Architectural Patterns & Principles
– Separation of Concerns:
• Presentation (components under app/(pages)/chat)
• Client State Management (Zustand stores)
• API Layer (Next.js API routes)
• Database Access Layer (lib/database_connect.js + models/)
• Middleware for cross‐cutting concerns (auth, rate‐limit)
– Real‐time event‐driven messaging with WebSockets (Socket.IO)
– Token‐based authentication (JWT)
– Environment‐driven configuration (via .env)