Implement SSE streaming for real-time scan progress updates with comprehensive testing suite and scanner service integration documentation#7
Conversation
Co-authored-by: lakshith-403 <37945916+lakshith-403@users.noreply.github.com>
Co-authored-by: lakshith-403 <37945916+lakshith-403@users.noreply.github.com>
There was a problem hiding this comment.
Pull Request Overview
This PR implements Server-Sent Events (SSE) support for real-time scan progress streaming in the Xployt main server. The implementation allows frontend applications to receive live updates during scan execution instead of relying on polling-based status checks.
Key changes include:
- Added SSE streaming endpoint
/api/v1/scans/{scan_id}/streamfor real-time scan progress updates - Enhanced scan service with SSE-enabled scanner service integration and proper error handling
- Created comprehensive documentation and test utilities for SSE functionality
Reviewed Changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| app/api/v1/endpoints/scans.py | Adds SSE streaming endpoint and enhances start_scan with use_sse parameter |
| app/services/scan_service.py | Implements SSE streaming, scanner service integration, and enhanced scan functionality |
| docs/SSE_INTEGRATION.md | Provides comprehensive documentation with frontend integration examples |
| docs/sse_test.html | Creates HTML test page for manual SSE functionality testing |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.
| except json.JSONDecodeError: | ||
| continue | ||
| else: | ||
| yield {"error": f"Scanner service returned status {response.status_code}"} |
There was a problem hiding this comment.
The httpx client.stream method does not support SSE streaming as shown. For SSE, you should use aiter_lines() on a regular POST response, not the stream context manager.
| yield {"error": f"Scanner service returned status {response.status_code}"} | |
| response = await client.post(scanner_url, json=scan_data) | |
| if response.status_code == 200: | |
| async for line in response.aiter_lines(): | |
| if line.startswith("data: "): | |
| try: | |
| data = json.loads(line[6:]) # Remove "data: " prefix | |
| yield data | |
| except json.JSONDecodeError: | |
| continue | |
| else: | |
| yield {"error": f"Scanner service returned status {response.status_code}"} |
|
@copilot how do i test if this works locally |
Co-authored-by: lakshith-403 <37945916+lakshith-403@users.noreply.github.com>
Here are several ways to test the SSE functionality locally: Quick Start (Easiest Method)1. Standalone SSE Test# Install dependencies and run the test script
pip install -r requirements.txt
python test_sse.pyThen open 2. Interactive Test Page# Start the main server
python run.pyThen visit Complete Setup (For Real API Testing)3. Full Server Setup# Install dependencies
pip install -r requirements.txt
# Set up environment (MongoDB optional for basic testing)
cp .env.sample .env
# Edit .env to add: MONGODB_URL=mongodb://localhost:27017
# Start server
python run.pyThen use the Swagger UI at
Frontend Integration Test// Test SSE connection in browser console
const eventSource = new EventSource('/test-sse');
eventSource.onmessage = (event) => {
console.log('SSE Data:', JSON.parse(event.data));
};I've added a comprehensive testing guide in The SSE implementation provides real-time progress updates with proper error handling, connection management, and structured JSON message format - commit 551116b. |
|
@copilot test_sse script doesn't work. the responses i get: but the logs i see, |
Co-authored-by: lakshith-403 <37945916+lakshith-403@users.noreply.github.com>
I've fixed the SSE test script issue! The problem was that the EventSourceResponse automatically handles SSE formatting, but we were manually adding "data: " prefixes, causing double-prefixed malformed data. Fix applied in commit 5c4a65d:
Test results: The SSE test now works perfectly with no JSON parsing errors. You can verify by running: python test_sse.py
# Visit http://localhost:3000 and click "Start SSE Test"The connection now shows clean progress updates, proper JSON parsing, and real-time visual updates with no errors. |
|
@copilot Give me a documentation to be used by the other services for them to send the events. Like, the interface |
…gration Co-authored-by: lakshith-403 <37945916+lakshith-403@users.noreply.github.com>
I've created comprehensive documentation for scanner services to integrate with the main server's SSE functionality. The new Scanner Service Protocol Specification provides: Interface Requirements:
Implementation Examples:
Key Interface Elements: # Request format
POST /scan
{
"scan_id": "507f1f77bcf86cd799439011",
"repository_name": "owner/repo",
"configurations": {...}
}
# SSE Response format
data: {"status": "scanning", "progress_percent": 45, "message": "Analyzing files...", "timestamp": "2025-01-20T10:30:00Z"}Testing & Validation:
The documentation is now available at |

This PR implements Server-Sent Events (SSE) streaming functionality to provide real-time scan progress updates to the frontend, addressing issue #6, with comprehensive local testing capabilities, fixes for data format issues, and complete documentation for scanner service integration.
Overview
The current scanning system only supported polling-based status updates, requiring frontend applications to repeatedly query the API for progress. This implementation adds SSE streaming capabilities that allow the main server to:
Key Changes
New SSE Streaming Endpoint
Added
GET /api/v1/scans/{scan_id}/streamthat provides real-time scan progress via Server-Sent Events:Enhanced Scanner Service Integration
Modified the scan service to support two modes:
use_sse=false): Original mock scanning for developmentuse_sse=true): Connects to real scanner services via SSEThe enhanced
POST /api/v1/scans/?use_sse=trueendpoint allows clients to choose between polling and streaming modes.Critical SSE Data Format Fix
Fixed a critical issue where malformed SSE data was causing JSON parsing errors in clients:
Problem: EventSourceResponse automatically handles SSE formatting, but manual "data: " prefixes were being added, resulting in double-prefixed malformed data.
Solution: Removed manual formatting to yield clean JSON data that EventSourceResponse handles correctly, enabling proper real-time updates with no parsing errors.
Comprehensive Scanner Service Protocol Documentation
Added complete documentation for external scanner services (like xployt-L2) to integrate with the main server's SSE functionality:
New Documentation:
Interface Requirements:
The documentation includes complete implementation examples, standardized status values, error handling patterns, and integration testing procedures.
Comprehensive Local Testing Suite
Added multiple testing methods to make local development and integration testing straightforward:
test_sse.py): Independent test server with visual demonstrationdocs/sse_test.html): Browser-based SSE testing with progress visualizationdocs/LOCAL_TESTING_GUIDE.md): Step-by-step setup and troubleshooting instructionsdocs/SCANNER_SERVICE_PROTOCOL.md): Complete interface specificationRobust Error Handling
Implemented comprehensive error handling including:
Working Demo
The SSE implementation now works correctly with proper JSON parsing and real-time updates:
Key features demonstrated:
Local Testing
Developers can now test SSE functionality using multiple methods:
Documentation
Backward Compatibility
All existing endpoints remain fully functional:
GET /api/v1/scans/{scan_id}- Polling-based status updatesGET /api/v1/scans/{scan_id}/results- Scan results retrievalFrontend applications can migrate to SSE incrementally while maintaining fallback support for polling.
Dependencies
Added
sse-starlette>=1.6.5for FastAPI SSE support.💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.