Aplikasi chat real-time Stack : React, Node.js/Express, Socket.io, dan MongoDB.
loginReact/
├── api/ # Backend (Node.js + Express)
│ ├── server.js
│ ├── model/
│ │ └── model.js # MongoDB Schemas (User, Chat, Group)
│ ├── controllers/
│ │ └── createUser.js
│ ├── package.json
│ ├── vercel.json # Vercel deployment config
│ ├── .env.example
│ └── .env # (buat sendiri, jangan commit!)
│
└── login/ # Frontend (React + Vite)
├── src/
│ ├── pages/
│ │ ├── Dhasboard.jsx
│ │ ├── LoginPage.jsx
│ │ └── RegisterPages.jsx
│ ├── service/
│ │ └── RequireLogin.jsx
│ └── ...
├── package.json
├── vercel.json
├── vite.config.js
├── .env.example
└── .env
- Node.js v14+
- MongoDB Atlas account (cloud database)
- Vercel account (untuk deploy)
- Git untuk version control
Buat file api/.env berdasarkan api/.env.example:
test
cd api
cp .env.example .envEdit api/.env:
# MongoDB Atlas Credentials
DB_USERNAME=your_mongodb_username
DB_PASSWORD=your_mongodb_password
# JWT Keys
AUTH_KEY=your_secret_jwt_key_here
ACC_TOKEN=access_token
# Frontend URL (CORS)
FRONTEND_URL=https://your-frontend-domain.vercel.app
# Environment
NODE_ENV=production
PORT=3000Cara mendapatkan MongoDB credentials:
- Buka MongoDB Atlas
- Create cluster → set username & password
- Get connection string → extract username & password
cd api
npm install -g vercel
vercel deploy --prodVercel akan menanyakan:
- Project name:
loginreact-api - Confirm settings: Y
Setelah deploy, Anda akan mendapat URL seperti: https://loginreact-api.vercel.app
-
Buka Vercel Dashboard
-
Pilih project
loginreact-api -
Settings → Environment Variables
-
Tambahkan:
DB_USERNAME= your MongoDB usernameDB_PASSWORD= your MongoDB passwordAUTH_KEY= random secret stringACC_TOKEN= token nameFRONTEND_URL= frontend domain (akan set setelah frontend deploy)NODE_ENV= production
-
Klik "Save & Redeploy"
Buat file login/.env berdasarkan login/.env.example:
cd login
cp .env.example .envEdit login/.env:
VITE_API_URL=https://loginreact-api.vercel.appcd login
npm run build
vercel deploy --prodSetelah deploy, Anda akan mendapat URL seperti: https://loginreact.vercel.app
Sekarang update FRONTEND_URL di backend dengan frontend domain:
- Buka Vercel Backend Project
- Settings → Environment Variables
- Edit
FRONTEND_URL=https://loginreact.vercel.app - Klik "Save & Redeploy"
- Buka aplikasi: https://loginreact.vercel.app
- Register account:
- Input email & password
- Click "Register"
- Check MongoDB Atlas → collection
usersterbuat dengan data baru
- Login:
- Gunakan email & password yang baru di-register
- Token JWT akan di-set di cookie (httpOnly, secure)
- Chat functionality:
- Buka di browser lain (atau private window)
- Register account berbeda
- Login dan chat antar akun
- Pesan akan tersimpan di MongoDB
chatcollection - Socket.io akan mengirim pesan real-time
- Create Group:
- Click plus button → Create Group
- Add group name & image
- Group akan tersimpan di MongoDB
groupscollection
- Access Group:
- Click nama group di sidebar
- POST request ke
/dhasboarddengangroupId
Dari Komputer:
Dari Smartphone:
- Buka: https://loginreact.vercel.app
- Responsive design akan auto-adjust
- Sidebars bisa di-swipe/toggle
Tips:
- Gunakan same WiFi atau buka di berbagai browser tabs
- DevTools → Device Emulation untuk test responsive
- ✅ Cookies:
httpOnly: true,secure: true(production) - ✅ CORS: Hanya menerima request dari frontend domain
- ✅ MongoDB: Username & password di environment variables (tidak di-commit)
- ✅ JWT: Secret key di environment (tidak di-commit)
- ✅ Database: MongoDB Atlas dengan IP whitelist
cd api
npm install dotenv- Check
DB_USERNAME&DB_PASSWORDdi.env - Check MongoDB Atlas IP whitelist (add 0.0.0.0/0 untuk Vercel)
- Check
FRONTEND_URLdi backend.env - Pastikan domain frontend sudah benar dan dipisah dengan
,jika multiple
- Check
VITE_API_URLdi frontend.env - Pastikan backend URL sudah production URL
- Pastikan di production:
secure: true,sameSite: 'lax' - Akses dari HTTPS (Vercel sudah auto HTTPS)
Untuk update production setelah perubahan code:
Backend:
cd api
git add .
git commit -m "fix: update feature"
git push
vercel deploy --prodFrontend:
cd login
npm run build
git add .
git commit -m "fix: update feature"
git push
vercel deploy --prodUntuk troubleshooting lebih lanjut:
- Check backend logs:
vercel logs loginreact-api -f - Check frontend logs: Browser DevTools → Console
- Check MongoDB: MongoDB Atlas → Collections
✅ User Registration & Login dengan JWT ✅ Real-time Chat dengan Socket.io ✅ Group Creation dengan Custom Image ✅ Responsive Mobile Design ✅ Cookie-based Session Management ✅ Multi-user Support ✅ Database: MongoDB Atlas ✅ Deployment: Vercel (Backend + Frontend)
Deployment Complete! Love 💖