Gamza is a modern real-time chat application built with React, Express, MongoDB, and Socket.IO. It features secure authentication, real-time messaging, and a clean architecture optimized for both development and production.
- React + Vite
- Component-based structure
- Located in
frontend/
- Express.js
- Mongoose (MongoDB ODM)
- Socket.IO for real-time messaging
- Located in
backend/
- Socket.IO server with JWT authentication
- Uses an HTTP-only cookie (
jwt) - Browser automatically includes the cookie during the WebSocket handshake
/backend → Express API, authentication, Socket.IO server, Mongoose models
/frontend → React app (Vite)
- Node.js v20+
- npm
- MongoDB (Atlas or local)
Create a .env file inside backend/:
MONGO_URL=your_mongodb_connection_string
JWT_SECRET=your_secret_key
PORT=3000
NODE_ENV=development
CLIENT_URL=http://localhost:5173
Gamza uses a secure HTTP-only cookie for authentication.
sameSite: "lax"secure: false- Works with the Vite dev server
sameSite: "none"secure: true- Required for cross-site cookies over HTTPS
npm install --prefix backend
npm install --prefix frontend(Use the example above)
$env:NODE_ENV='development';
$env:CLIENT_URL='http://localhost:5173';
npm run start --prefix backendnpm run dev --prefix frontend- Signup
- Login
- Logout
- Update profile
- Get contacts
- Get chats
- Get messages
- Send a message
- Implemented in
backend/src/lib/socket.js - Uses
socketAuthMiddlewareto validate JWT cookies during connection
io(BASE_URL, { withCredentials: true });getOnlineUsers— broadcast list of online usersnewMessage— real-time delivery of messages
- Client sends message via REST:
POST /api/messages/v1/send/:id - Backend saves it to MongoDB
- If the receiver is online → server emits
newMessage
DevTools → Network → /socket.io/
- Ensure the cookie header includes
jwt
Look for socket auth errors (invalid token, missing cookie, etc.).
- Dev:
sameSite="lax",secure=false - Prod:
sameSite="none",secure=true
Ensure JWT_SECRET matches the token signer.
jwtcookie exists (DevTools → Application → Cookies)/socket.io/request includes cookie- No socket auth errors in backend logs
CLIENT_URLcorrect in.env
I can help you add:
backend/.env.example- Root
devscript to run frontend + backend together - Automatic cookie setup based on
NODE_ENV - More logging for socket connections