Skip to content

Shreyanshjain3725/Traffic-Management

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

Traffic Management System

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.

Features

  • 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

Tech Stack

  • MongoDB – Intersections, lanes, traffic logs
  • Express – REST API and Socket.io server
  • React – Dashboard, intersections, logs (Vite)
  • Node.js – Backend runtime

Prerequisites

  • Node.js 18+
  • MongoDB (local or Atlas)

Database setup (do this before running the backend)

Choose one option: local MongoDB or MongoDB Atlas (cloud).

Option A: MongoDB on your computer (local)

  1. Install MongoDB

    • Windows: Download from mongodb.com/try/download/community and run the installer, or use Chocolatey: choco install mongodb
    • macOS: brew install mongodb-community then brew services start mongodb-community
    • Linux: Use your package manager (e.g. sudo apt install mongodb on Ubuntu)
  2. Start MongoDB

    • Windows: If installed as a service, it may start automatically. Otherwise run mongod from the install directory (e.g. C:\Program Files\MongoDB\Server\7.0\bin\mongod.exe).
    • macOS/Linux: mongod (or sudo systemctl start mongod if using systemd).
  3. Create the .env file in the backend folder (if you haven’t):

    cd backend
    cp .env.example .env

    The default MONGODB_URI=mongodb://localhost:27017/traffic_management is correct for local MongoDB. The database name traffic_management and the collections will be created automatically when the app runs.

Option B: MongoDB Atlas (cloud, no local install)

  1. Go to mongodb.com/cloud/atlas and create a free account.
  2. Create a free cluster (e.g. M0).
  3. Create a database user: Security → Database Access → Add New User (username + password). Remember the password.
  4. Allow network access: Security → Network Access → Add IP Address → allow 0.0.0.0 (or your IP) so your app can connect.
  5. 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
    
  6. 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
    
  7. In the backend folder, create or edit .env and set:
    MONGODB_URI=mongodb+srv://myuser:mypass@cluster0.xxxxx.mongodb.net/traffic_management?retryWrites=true&w=majority
    
    (Use your real URI; special characters in the password must be URL-encoded.)

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.


Setup

1. Backend

cd backend
npm install
cp .env.example .env

Edit backend/.env:

  • MONGODB_URI – Local: mongodb://localhost:27017/traffic_management (default). Atlas: paste your connection string from Database setup above.
  • PORT – default 5000
  • CLIENT_URL – default http://localhost:5173

Seed sample intersections (optional, after backend can connect):

cd backend
npm run seed

Start the backend server (required; the frontend proxies API to this):

npm run dev

You should see Server running on http://localhost:5000. Keep this terminal open.

2. Frontend

cd frontend
npm install

Start the dev server (proxies API and Socket.io to backend):

npm run dev

Open http://localhost:5173.

Important: Run both backend and frontend:

  • Terminal 1: cd backendnpm run dev (API on port 5000)
  • Terminal 2: cd frontendnpm 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.

Troubleshooting

ECONNREFUSED or "http proxy error: /api/..."

The backend is not running. The frontend proxies /api to http://localhost:5000.

  1. Open a second terminal.
  2. Run: cd backend then npm run dev.
  3. Ensure you see Server running on http://localhost:5000.
  4. Reload the frontend; the proxy errors should stop.

MongoDB connection failed

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.

Usage

  1. Dashboard – View all intersections, current signal, and density. Edit density/vehicles per lane to simulate traffic; adaptive logic updates green duration.
  2. Intersections – Add/delete intersections. Each has 4 directions with density bars and inline edit.
  3. Traffic Logs – Filter by intersection and see past density/signal snapshots.

API Overview

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.

Project Structure

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

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors