Skip to main content

Backend Overview

Express API server providing REST endpoints and WebSocket real-time updates for traffic monitoring, integrating with Stellio Context Broker, Neo4j, Fuseki, and PostgreSQL.

Architecture​

graph TB
subgraph "Express Backend :5000"
A[server.ts] --> B[Routes]
A --> C[Services]
A --> D[Agents]
A --> E[WebSocket Server]

B --> B1[Camera Routes]
B --> B2[Weather Routes]
B --> B3[AirQuality Routes]
B --> B4[Accident Routes]
B --> B5[Pattern Routes]
B --> B6[Analytics Routes]
B --> B7[Agent Routes]

C --> C1[StellioService]
C --> C2[Neo4jService]
C --> C3[FusekiService]
C --> C4[PostgresService]
C --> C5[WebSocketService]
C --> C6[DataAggregator]

D --> D1[EcoTwinAgent]
D --> D2[GraphInvestigatorAgent]
D --> D3[TrafficMaestroAgent]
end

subgraph "External Services"
F[(Stellio Context Broker)]
G[(Neo4j Graph DB)]
H[(Fuseki SPARQL)]
I[(PostgreSQL/TimescaleDB)]
J[Google Gemini AI]
K[Tavily Search API]
end

C1 --> F
C2 --> G
C3 --> H
C4 --> I
D1 --> J
D2 --> J
D2 --> K
D3 --> J

Key Features​

RESTful API​

  • 12 endpoint groups covering all traffic data types
  • CORS-enabled for frontend integration
  • Request validation with structured error handling
  • YAML-driven configuration (config/entities.yaml)

Real-Time Updates​

  • WebSocket server for bidirectional communication
  • Topic-based pub/sub (cameras, weather, accidents, etc.)
  • Heartbeat mechanism with 30s timeout
  • Automatic reconnection handling

AI Agents​

  • EcoTwin: Environmental health advisor with AQI dispersion modeling
  • GraphInvestigator: Multimodal incident analysis with GraphRAG
  • TrafficMaestro: Predictive event-based congestion forecasting

Multi-Database Integration​

  • Stellio: NGSI-LD entities (primary data source)
  • Neo4j: Graph relationships and correlations
  • Fuseki: SPARQL queries and LOD enrichment
  • PostgreSQL: Temporal data and analytics

Source Structure​

backend/src/
Ò”œÒ”€Ò”€ server.ts # Main Express server entry point
Ò”œÒ”€Ò”€ agents/ # AI agents (3 files)
Γ’β€β€š Ò”œÒ”€Ò”€ EcoTwinAgent.ts
Γ’β€β€š Ò”œÒ”€Ò”€ GraphInvestigatorAgent.ts
Γ’β€β€š Ò””Ò”€Ò”€ TrafficMaestroAgent.ts
Ò”œÒ”€Ò”€ config/ # Configuration loader
Γ’β€β€š Ò””Ò”€Ò”€ configLoader.ts
Ò”œÒ”€Ò”€ middlewares/ # Express middlewares
Γ’β€β€š Ò””Ò”€Ò”€ errorHandler.ts
Ò”œÒ”€Ò”€ routes/ # API route handlers (12 files)
Γ’β€β€š Ò”œÒ”€Ò”€ cameraRoutes.ts
Γ’β€β€š Ò”œÒ”€Ò”€ weatherRoutes.ts
Γ’β€β€š Ò”œÒ”€Ò”€ airQualityRoutes.ts
Γ’β€β€š Ò””Ò”€Ò”€ ...
Ò”œÒ”€Ò”€ services/ # Data services (7 files)
Γ’β€β€š Ò”œÒ”€Ò”€ stellioService.ts
Γ’β€β€š Ò”œÒ”€Ò”€ neo4jService.ts
Γ’β€β€š Ò”œÒ”€Ò”€ fusekiService.ts
Γ’β€β€š Ò””Ò”€Ò”€ ...
Ò”œÒ”€Ò”€ types/ # TypeScript definitions
Γ’β€β€š Ò””Ò”€Ò”€ index.ts
Ò””Ò”€Ò”€ utils/ # Utility functions (5 files)
Ò”œÒ”€Ò”€ logger.ts
Ò”œÒ”€Ò”€ healthCheck.ts
Ò””Ò”€Ò”€ ...

Technology Stack​

ComponentTechnologyVersion
RuntimeNode.js18+
FrameworkExpress4.18+
LanguageTypeScript5.1+
WebSocketws8.14+
Database Clientsneo4j-driver, pg, sparql-http-client-
AI SDK@google/generative-ai0.21+
LoggingWinston3.11+

Environment Configuration​

# Server
PORT=5000
NODE_ENV=development

# CORS
CORS_ORIGIN=http://localhost:5173,http://localhost:3000

# Stellio Context Broker
STELLIO_URL=http://localhost:8080
STELLIO_NGSI_LD_PATH=/ngsi-ld/v1

# Neo4j
NEO4J_URL=bolt://localhost:7687
NEO4J_USER=neo4j
NEO4J_PASSWORD=test12345

# Fuseki
FUSEKI_URL=http://localhost:3030
FUSEKI_DATASET=lod-dataset

# PostgreSQL
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_DB=stellio_search

# AI APIs
GEMINI_API_KEY=your_key_here
TAVILY_API_KEY=your_key_here

Quick Start​

# Install dependencies
cd apps/traffic-web-app/backend
npm install

# Development mode
npm run dev

# Production build
npm run build
npm start

# Run tests
npm test

API Endpoints​

GroupBase PathDescription
Cameras/api/camerasTraffic camera entities
Weather/api/weatherWeather observations
Air Quality/api/air-qualityAQI and pollutant data
Accidents/api/accidentsRoad accident events
Patterns/api/patternsTraffic congestion patterns
Analytics/api/analyticsAggregated metrics
Historical/api/historicalTime-series data
Correlation/api/correlationEntity relationships
Routing/api/routingRoute planning
Geocoding/api/geocodingAddress conversion
Agents/api/agentsAI agent queries
Multi-Agent/api/multi-agentCombined agent analysis

Health Check​

curl http://localhost:5000/health

Returns connectivity status for all backend services.

See Also​