perf: Optimize SSE communication between browser and container
All checks were successful
build-and-deploy / deploy (push) Successful in 9s
build-and-deploy / build (push) Successful in 8s
build-and-deploy / filter (push) Successful in 2s

- Add anti-buffering headers (X-Accel-Buffering, Content-Encoding)
- Reduce polling interval from 500ms to 250ms for faster updates
- Add heartbeat mechanism to keep connection alive
- Implement auto-reconnection on client side
- Disable Express response buffering for SSE endpoints
- Skip empty heartbeat messages on client
- Improve error handling and logging
This commit is contained in:
2025-07-05 17:02:19 -06:00
parent 7de915166f
commit 1b3d43cfea
2 changed files with 39 additions and 4 deletions

View File

@@ -47,6 +47,8 @@ class AdminService {
await this.initializeServerUrl()
this.callback = callback
// Configure EventSource with better error handling
this.eventSource = new EventSource('/api/sse')
this.eventSource.onopen = () => {
@@ -57,11 +59,14 @@ class AdminService {
this.eventSource.onmessage = (event) => {
try {
// Skip heartbeat messages
if (event.data.trim() === '') return
const data = JSON.parse(event.data)
console.log('[AdminService] SSE message received:', data.type, data.timestamp)
this.callback?.(data)
} catch (error) {
console.error('[AdminService] Error parsing SSE message:', error)
console.error('[AdminService] Error parsing SSE message:', error, 'Raw data:', event.data)
}
}
@@ -69,6 +74,14 @@ class AdminService {
console.error('[AdminService] SSE connection error:', error)
this.isConnected = false
this.connectionCallback?.(false)
// Auto-reconnect after 5 seconds
setTimeout(() => {
if (!this.isConnected) {
console.log('[AdminService] Attempting to reconnect...')
this.connect(this.callback!)
}
}, 5000)
}
}