The application now automatically detects whether it's running in development (localhost) or production and uses the appropriate backend URLs.
The app checks window.location.hostname:
- Development:
localhostor127.0.0.1→ Useshttp://localhost:5000 - Production: Any other domain → Uses
https://chatapp-backend-1-2k8y.onrender.com
All environment settings are centralized in /src/config/environment.js:
{
apiUrl: "http://localhost:5000" (dev) or production URL,
socketUrl: "http://localhost:5000" (dev) or production URL,
isDevelopment: true/false,
isProduction: true/false
}-
src/context/SocketContext.jsx- Now uses
config.socketUrlfrom environment config - Automatically connects to correct socket server based on environment
- Logs the socket URL to console for debugging
- Now uses
-
src/config/environment.js(NEW)- Centralized configuration for all environment-based URLs
- Can be imported anywhere in the app
-
.env.example- Added
VITE_SOCKET_URLoption for manual override
- Added
Just run your app normally:
npm run devThe app will automatically use:
- API:
http://localhost:5000 - Socket:
http://localhost:5000
When deployed to Vercel or any production domain, it will automatically use:
- API:
https://chatapp-backend-1-2k8y.onrender.com - Socket:
https://chatapp-backend-1-2k8y.onrender.com
Create a .env.local file to override defaults:
VITE_SOCKET_URL=http://localhost:5000
VITE_API_URL=http://localhost:5000Or for a different backend:
VITE_SOCKET_URL=http://localhost:8080
VITE_API_URL=http://localhost:8080For production deployment on Vercel, you can set environment variables:
- Go to Vercel Dashboard → Your Project → Settings → Environment Variables
- Add:
VITE_SOCKET_URL=https://chatapp-backend-1-2k8y.onrender.comVITE_API_URL=https://chatapp-backend-1-2k8y.onrender.com
- Open browser DevTools (F12)
- Go to Console tab
- Look for:
"Connecting to socket: http://localhost:5000"(dev) or production URL - Check Network tab → WS (WebSocket) to see active socket connection
Add this to any component to check current config:
import config from '../config/environment';
console.log('Current config:', config);- Make sure your backend is running on
http://localhost:5000 - Check console for "Connecting to socket:" message
- Verify the URL is correct
- Check Vercel environment variables
- Verify backend URL is accessible
- Check CORS settings on backend
If your backend runs on a different port (e.g., 8080):
- Create
.env.local:VITE_SOCKET_URL=http://localhost:8080 VITE_API_URL=http://localhost:8080
- Update
vite.config.jsproxy target to match
✅ No manual switching - Automatically detects environment
✅ Centralized config - All URLs in one place
✅ Easy override - Use .env.local for custom settings
✅ Production ready - Works seamlessly on Vercel
✅ Debugging friendly - Logs socket URL to console