A MERN stack application that monitors traffic conditions and controls traffic signals in real time. The system uses live density data to adjust signal timings for smoother traffic flow.
- Real-time monitoring – Dashboard shows traffic density and signal state per intersection
- Adaptive signals – Green duration is computed from traffic density (10–90 seconds)
- Per-lane density – North, South, East, West lanes with editable density and vehicle count
- Traffic logs – Historical snapshots of density and signal state
- WebSocket updates – Signal changes pushed to the UI without refresh
- MongoDB – Intersections, lanes, traffic logs
- Express – REST API and Socket.io server
- React – Dashboard, intersections, logs (Vite)
- Node.js – Backend runtime
- Node.js 18+
- MongoDB (local or Atlas)
Choose one option: local MongoDB or MongoDB Atlas (cloud).
-
Install MongoDB
- Windows: Download from mongodb.com/try/download/community and run the installer, or use Chocolatey:
choco install mongodb - macOS:
brew install mongodb-communitythenbrew services start mongodb-community - Linux: Use your package manager (e.g.
sudo apt install mongodbon Ubuntu)
- Windows: Download from mongodb.com/try/download/community and run the installer, or use Chocolatey:
-
Start MongoDB
- Windows: If installed as a service, it may start automatically. Otherwise run
mongodfrom the install directory (e.g.C:\Program Files\MongoDB\Server\7.0\bin\mongod.exe). - macOS/Linux:
mongod(orsudo systemctl start mongodif using systemd).
- Windows: If installed as a service, it may start automatically. Otherwise run
-
Create the
.envfile in the backend folder (if you haven’t):cd backend cp .env.example .envThe default
MONGODB_URI=mongodb://localhost:27017/traffic_managementis correct for local MongoDB. The database nametraffic_managementand the collections will be created automatically when the app runs.
- Go to mongodb.com/cloud/atlas and create a free account.
- Create a free cluster (e.g. M0).
- Create a database user: Security → Database Access → Add New User (username + password). Remember the password.
- Allow network access: Security → Network Access → Add IP Address → allow
0.0.0.0(or your IP) so your app can connect. - Get the connection string: Database → Connect → “Connect your application”. Copy the URI; it looks like:
mongodb+srv://<username>:<password>@cluster0.xxxxx.mongodb.net/?retryWrites=true&w=majority - In the URI, replace
<username>and<password>with your DB user. Add the database name before?:mongodb+srv://myuser:mypass@cluster0.xxxxx.mongodb.net/traffic_management?retryWrites=true&w=majority - In the backend folder, create or edit
.envand set:(Use your real URI; special characters in the password must be URL-encoded.)MONGODB_URI=mongodb+srv://myuser:mypass@cluster0.xxxxx.mongodb.net/traffic_management?retryWrites=true&w=majority
No need to create the database or collections manually. The app creates the traffic_management database and the intersections and trafficlogs collections when it runs.
cd backend
npm install
cp .env.example .envEdit backend/.env:
MONGODB_URI– Local:mongodb://localhost:27017/traffic_management(default). Atlas: paste your connection string from Database setup above.PORT– default5000CLIENT_URL– defaulthttp://localhost:5173
Seed sample intersections (optional, after backend can connect):
cd backend
npm run seedStart the backend server (required; the frontend proxies API to this):
npm run devYou should see Server running on http://localhost:5000. Keep this terminal open.
cd frontend
npm installStart the dev server (proxies API and Socket.io to backend):
npm run devOpen http://localhost:5173.
Important: Run both backend and frontend:
- Terminal 1:
cd backend→npm run dev(API on port 5000) - Terminal 2:
cd frontend→npm run dev(app on port 5173)
If MongoDB is not running, start it (e.g. mongod) or use a cloud URI in .env. The backend will still listen; API calls will fail until MongoDB is connected.
The backend is not running. The frontend proxies /api to http://localhost:5000.
- Open a second terminal.
- Run:
cd backendthennpm run dev. - Ensure you see
Server running on http://localhost:5000. - Reload the frontend; the proxy errors should stop.
Start MongoDB locally (mongod) or set MONGODB_URI in backend/.env to a valid URI (e.g. MongoDB Atlas). The backend stays up; fix the DB and retry.
- Dashboard – View all intersections, current signal, and density. Edit density/vehicles per lane to simulate traffic; adaptive logic updates green duration.
- Intersections – Add/delete intersections. Each has 4 directions with density bars and inline edit.
- Traffic Logs – Filter by intersection and see past density/signal snapshots.
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/intersections |
List intersections |
| GET | /api/intersections/:id |
Get one intersection |
| POST | /api/intersections |
Create intersection |
| PUT | /api/intersections/:id |
Update intersection |
| DELETE | /api/intersections/:id |
Delete intersection |
| PATCH | /api/intersections/:id/density |
Update lane density (body: { laneUpdates }) |
| PATCH | /api/intersections/:id/signal |
Set active signal and state |
| GET | /api/intersections/logs |
Traffic logs (query: intersectionId, limit) |
Real-time: Socket.io emits signal-update when any intersection’s signal state changes.
Traffic Management/
├── backend/
│ ├── config/db.js
│ ├── controllers/intersectionController.js
│ ├── models/Intersection.js, TrafficLog.js
│ ├── routes/intersectionRoutes.js
│ ├── utils/trafficLogic.js
│ ├── scripts/seed.js
│ └── server.js
├── frontend/
│ ├── src/
│ │ ├── api/api.js
│ │ ├── context/SocketContext.jsx
│ │ ├── components/
│ │ ├── pages/
│ │ └── App.jsx, main.jsx
│ └── vite.config.js
└── README.md
MIT