How krrishkumar125/ridewave-real-time-ride-matching-system works
Project Type & Purpose
• RideWave is a monolithic, real-time backend service for ride-hailing. It exposes both HTTP REST endpoints (via Express) and WebSocket channels (via Socket.IO) to match passengers with drivers using geospatial data stored in Redis and persistent data in MongoDB.
File Structure
• Top-level files:
• package.json / package-lock.json – Node.js dependencies & scripts
• Readme.md – project overview
• src/index.js – application entry point: starts Express HTTP server + Socket.IO
• src/routes/ – defines HTTP routes (auth, booking, driver, passenger)
• src/controllers/ – orchestrates incoming requests / socket events
• src/services/ – business logic (booking matching, location lookups, auth flows)
• src/repository/ – data-access layer for MongoDB (via Mongoose models) and Redis (via geo and key–value)
• src/models/ – Mongoose schemas: user, booking
• src/utils/ – DB connectors (Mongo, Redis), distance calculation
• src/public/ – static frontend (driver.html) served by Express
• src/middlewares/ – JWT auth (ready for future use)
README Insights
• Tech stack: Node.js/Express, MongoDB, Redis (GEO), Socket.IO
• Key features: real-time socket events (newBooking, rideConfirmed, removeBooking), geospatial proximity matching, Redis caching & indexing, JWT authentication.
• System overview hints at layered architecture and heavy reliance on Redis GEO commands for performance.
Main Components
a. External Clients
– Passenger app (HTTP + WebSocket)
– Driver app (HTTP + WebSocket)
b. API Layer (Express)
– HTTP Server (routes → controllers)
– WebSocket Server (socket events handler in index.js)
c. Controller Layer
– authController, bookingController, driverController, passengerController
d. Service Layer
– authService (JWT), bookingService (lifecycle), driverService, passengerService, locationService (distance & geospatial logic)
e. Repository / Data Access
– MongoDB via bookingRepository.js & driverRepository.js & Mongoose models
– Redis via redisClient.js & GEO operations (driver socketId map, proximity search)
f. Data Stores & External Systems
– MongoDB (persistent storage for users & bookings)
– Redis (caching, driver ↔ socketId map, GEO indexing)
g. Static File Server
– Serves src/public/driver.html for driver UI (optional)
Relationships & Data Flows
• HTTP Request Flow:
Client → Express Router → Controller → Service → Repository → MongoDB → Service → Controller → Response → Client
• WebSocket Flow (e.g., newBooking):
Passenger emits newBooking → Socket.IO handler (index.js) → bookingController → bookingService:
• Save booking in MongoDB & push to Redis GEO index
• Query Redis GEO to find nearest drivers
• Emit rideRequest to matched driver sockets
• Redis GEO & Key–Value:
– driverService updates driver location & socketId in Redis
– bookingService issues GEORADIUS queries
• Middleware:
– authMiddleware enforces JWT validation on protected HTTP routes
Architectural Patterns & Principles
• Layered Architecture (Router → Controller → Service → Repository → Database)
• Separation of Concerns (e.g., controllers don’t access DB directly)
• Repository Pattern abstracts data stores (Mongo vs Redis)
• Event-Driven via WebSockets for real-time updates
• Caching & Geospatial Indexing in Redis for performance
• JWT-based authentication (middleware)