Skip to content

Feature/websockets#1

Open
spicyneutrino wants to merge 2 commits intoXGraph-Team:mainfrom
spicyneutrino:feature/websockets
Open

Feature/websockets#1
spicyneutrino wants to merge 2 commits intoXGraph-Team:mainfrom
spicyneutrino:feature/websockets

Conversation

@spicyneutrino
Copy link

@spicyneutrino spicyneutrino commented Feb 2, 2026

Pull Request

📋 Description

This PR completes the migration from an HTTP polling architecture to a real-time WebSocket push model. It eliminates periodic GET requests for Network State, V2G Status, AI Map Focus, and Scenario Controller updates, replacing them with event-driven system_update and v2g_restoration_complete WebSocket events.

This change significantly reduces network overhead (~5-7 req/sec per client eliminated) and improves the responsiveness of the frontend application by delivering updates in real-time rather than on fixed polling intervals.

Fixes #(ISSUE_NUMBER)

🔄 Type of Change

Please delete options that are not relevant.

  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 🧹 Code refactoring (no functional changes)
  • ⚡ Performance improvement

Note: While this changes the data flow architecture, the WebSocket infrastructure was already in place. The frontend now relies on push-based updates instead of polling, but legacy API endpoints remain available for backward compatibility.

🧪 Testing

Manual verification performed to ensure real-time data synchronization between backend and frontend without polling.

  • Manual testing
  • Network traffic analysis
  • Unit tests
  • Integration tests
  • End-to-end testing

Test Configuration:

  • Python version: 3.12
  • SUMO version: 1.15+
  • Operating system: Linux (Fedora/Bluefin)
  • Browser: Chrome (latest)

Verification Methods:

  1. ✅ Hard refresh (Ctrl+Shift+R) to clear browser cache
  2. ✅ Incognito mode testing to verify clean state
  3. ✅ Network tab analysis showing 0 polling requests
  4. ✅ WebSocket connection verified active in DevTools
  5. ✅ Real-time updates confirmed for V2G, scenarios, and map focus

📸 Screenshots (if applicable)

Before:
(Network tab showing continuous polling requests every 1-3 seconds)

Before - Continuous HTTP polling Before - Multiple GET requests

After:
(Network tab showing 0 polling requests - only WebSocket traffic and initial page load)

After - No polling requests After - Clean network tab

✅ Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published

🔧 Implementation Details

Changes Made

  • main_complete_integration.py: Enhanced broadcast_state() to include V2G active vehicle data and AI map focus coordinates in the system_update payload.
  • v2g_manager.py: Implemented v2g_websocket_callback() to bridge V2G manager events with WebSocket emissions and registered it for real-time restoration notifications.
  • static/script.js: Removed updateLoop() (2s interval) and updateV2GColorsOptimized() (5s interval) polling loops. Added updateV2GFromWebSocket() and applyAIMapFocus() listeners.
  • static/scenario-director.js: Removed startChatbotMonitoring() polling loop and added WebSocket listener for v2g_restoration_complete events.
  • index.html: Updated cache-busting query parameters (?v=nocache_<timestamp>) to force browser reload of updated JavaScript files.

Architecture Impact

  • API: The following endpoints are no longer polled periodically: /api/network_state, /api/v2g/status, /api/ai/map_focus_status.
  • V2G System: Now utilizes an event-driven callback system for restoration status rather than passive state checks.
  • WebSocket Events: system_update now carries V2G and AI focus data; new v2g_restoration_complete event added.

Performance Considerations

  • Network Traffic: Eliminated ~5-7 HTTP GET requests per second per client.
  • CPU Usage: Reduced client-side main thread blocking by removing recursive setTimeout and setInterval calls.
  • Responsiveness: Improved real-time responsiveness (instant updates vs 2-5 second polling delays).

🔄 Migration Notes

  1. Browser Cache: Users may need to perform a hard refresh (Ctrl+Shift+R / Cmd+Shift+R) after deployment.
  2. WebSocket Requirement: Frontend now relies on persistent WebSocket connection; ensure WebSocket infrastructure is stable.
  3. Legacy Support: Old API endpoints remain available for manual debugging or one-time queries.

📋 Dependencies

No new dependencies required. Utilizes existing infrastructure (Socket.IO, Flask-SocketIO).

🔗 Related Issues/PRs

  • Addresses WebSocket migration initiative
  • Closes #[INSERT_ISSUE_NUMBER]

📚 Documentation

  • README updated
  • API documentation updated
  • Inline code comments added
  • Tutorial/guide updated
  • CHANGELOG updated

🚀 Deployment Notes

  • Requires environment variable changes
  • Requires database migration
  • Requires configuration updates
  • Requires external service setup

🧑‍💻 Reviewer Notes

Please pay special attention to:

  • Performance impact: Verify Network tab shows no recurring polling requests (see screenshots).
  • Race Conditions: Ensure v2g_restoration_complete events don't arrive before frontend initialization.
  • WebSocket Data Flow: Confirm system_update events contain V2G and AI focus data.
  • Error handling: Verify WebSocket reconnection logic handles new message types gracefully.

📝 Additional Notes

This PR represents the complete elimination of HTTP polling. The backend and frontend are now fully event-driven for Network, V2G, and Scenario status.

- Enhanced backend broadcast_state to push V2G and AI focus data via WebSocket
- Removed all frontend setInterval polling loops in script.js
- Implemented WebSocket event listeners for system_update and v2g_restoration_complete
- Refactored scenario-director to use event-driven status checks
- Updated index.html cache busting to force reload of new scripts

Performance: Eliminated ~5-7 req/sec from periodic polling of /api/network_state, /api/v2g/status, and /api/ai/map_focus_status
- Added WebSocket callback to scenario_controller for real-time updates
- Replaced setInterval polling in scenario-controls.js with WebSocket listener
- Added handleSystemUpdate() to process scenario data from system_update events
- Removed 3-second polling loop for scenario status updates
Part of: WebSocket migration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant