File size: 7,873 Bytes
c27ae8d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# WebSocket Protocol for Real-Time Updates
## Overview
WebSocket connections provide real-time progress updates from backend workers to the frontend during transcription.
## Why WebSocket?
- **Push-Based**: Server pushes updates, no client polling
- **Low Latency**: ~10-50ms vs. 1000ms for HTTP polling
- **Efficient**: Single persistent connection vs. repeated HTTP requests
- **Real-Time UX**: Smooth progress bar, stage updates
---
## Connection Lifecycle
```mermaid
sequenceDiagram
participant Client
participant Server
participant Worker
Client->>Server: 1. Connect WS ws://localhost:8000/api/v1/jobs/{job_id}/stream
Server->>Client: 2. Connection established
Worker->>Server: 3. Send updates
Server->>Client: Broadcast updates
Worker->>Server: 4. Job complete
Server->>Client: Send "completed" message
Client->>Server: 5. Close connection
```
---
## Message Types
### 1. Progress Update
**Sent by**: Worker during processing
**Frequency**: Every 5-10% progress or stage change
```json
{
"type": "progress",
"job_id": "550e8400-e29b-41d4-a716-446655440000",
"progress": 45,
"stage": "separation",
"message": "Separated drums stem (2/4)",
"timestamp": "2025-01-15T10:30:45Z"
}
```
**Fields**:
- `progress`: 0-100
- `stage`: "download" | "separation" | "transcription" | "musicxml"
- `message`: Human-readable status
---
### 2. Completion
**Sent by**: Worker when job finishes successfully
```json
{
"type": "completed",
"job_id": "550e8400-e29b-41d4-a716-446655440000",
"progress": 100,
"result_url": "/api/v1/scores/550e8400-e29b-41d4-a716-446655440000",
"duration_seconds": 125,
"timestamp": "2025-01-15T10:32:15Z"
}
```
**Client Action**: Fetch MusicXML from `result_url`
---
### 3. Error
**Sent by**: Worker when job fails
```json
{
"type": "error",
"job_id": "550e8400-e29b-41d4-a716-446655440000",
"error": {
"message": "GPU out of memory during source separation",
"retryable": true,
"stage": "separation"
},
"timestamp": "2025-01-15T10:31:00Z"
}
```
**Client Action**: Show error, optionally retry if `retryable: true`
---
### 4. Heartbeat (Keep-Alive)
**Sent by**: Server every 30 seconds
**Purpose**: Detect dropped connections
```json
{
"type": "heartbeat",
"timestamp": "2025-01-15T10:30:00Z"
}
```
**Client Action**: Send "pong" response
```json
{
"type": "pong",
"timestamp": "2025-01-15T10:30:00Z"
}
```
---
## Frontend Implementation
### WebSocket Hook
```typescript
import { useEffect, useState } from 'react';
interface ProgressUpdate {
type: 'progress' | 'completed' | 'error';
progress: number;
stage?: string;
message?: string;
result_url?: string;
error?: { message: string; retryable: boolean };
}
export function useJobProgress(jobId: string) {
const [progress, setProgress] = useState(0);
const [status, setStatus] = useState<'connecting' | 'processing' | 'completed' | 'failed'>('connecting');
const [error, setError] = useState<string | null>(null);
useEffect(() => {
const ws = new WebSocket(`ws://localhost:8000/api/v1/jobs/${jobId}/stream`);
ws.onopen = () => {
console.log('WebSocket connected');
setStatus('processing');
};
ws.onmessage = (event) => {
const update: ProgressUpdate = JSON.parse(event.data);
switch (update.type) {
case 'progress':
setProgress(update.progress);
break;
case 'completed':
setProgress(100);
setStatus('completed');
// Fetch score
fetchScore(update.result_url!);
break;
case 'error':
setStatus('failed');
setError(update.error!.message);
break;
case 'heartbeat':
// Send pong
ws.send(JSON.stringify({ type: 'pong', timestamp: new Date().toISOString() }));
break;
}
};
ws.onerror = (error) => {
console.error('WebSocket error:', error);
setStatus('failed');
setError('Connection error');
};
ws.onclose = () => {
console.log('WebSocket closed');
};
return () => {
ws.close();
};
}, [jobId]);
return { progress, status, error };
}
```
---
## Backend Implementation (FastAPI)
### Connection Manager
```python
from fastapi import WebSocket
from typing import Dict, List
import json
class ConnectionManager:
def __init__(self):
self.active_connections: Dict[str, List[WebSocket]] = {}
async def connect(self, websocket: WebSocket, job_id: str):
await websocket.accept()
if job_id not in self.active_connections:
self.active_connections[job_id] = []
self.active_connections[job_id].append(websocket)
def disconnect(self, websocket: WebSocket, job_id: str):
if job_id in self.active_connections:
self.active_connections[job_id].remove(websocket)
async def broadcast(self, job_id: str, message: dict):
"""Send message to all clients connected to this job."""
if job_id in self.active_connections:
dead_connections = []
for connection in self.active_connections[job_id]:
try:
await connection.send_json(message)
except:
dead_connections.append(connection)
# Clean up dead connections
for conn in dead_connections:
self.disconnect(conn, job_id)
manager = ConnectionManager()
```
### WebSocket Endpoint
```python
@app.websocket("/api/v1/jobs/{job_id}/stream")
async def websocket_endpoint(websocket: WebSocket, job_id: str):
await manager.connect(websocket, job_id)
try:
# Subscribe to Redis pub/sub for this job
pubsub = redis_client.pubsub()
pubsub.subscribe(f"job:{job_id}:updates")
async for message in pubsub.listen():
if message['type'] == 'message':
update = json.loads(message['data'])
await websocket.send_json(update)
# Close connection if job completed or failed
if update.get('type') in ['completed', 'error']:
break
except WebSocketDisconnect:
manager.disconnect(websocket, job_id)
finally:
pubsub.unsubscribe(f"job:{job_id}:updates")
```
---
## Error Handling
### Reconnection Strategy
```typescript
let reconnectAttempts = 0;
const MAX_RECONNECTS = 5;
function connectWithRetry(jobId: string) {
const ws = new WebSocket(`ws://localhost:8000/api/v1/jobs/${jobId}/stream`);
ws.onerror = () => {
if (reconnectAttempts < MAX_RECONNECTS) {
reconnectAttempts++;
const delay = Math.min(1000 * 2 ** reconnectAttempts, 10000); // Exponential backoff
setTimeout(() => connectWithRetry(jobId), delay);
} else {
// Fallback to polling
pollJobStatus(jobId);
}
};
}
```
### Fallback to Polling
If WebSocket fails, fall back to HTTP polling:
```typescript
function pollJobStatus(jobId: string) {
const interval = setInterval(async () => {
const response = await fetch(`/api/v1/jobs/${jobId}`);
const job = await response.json();
setProgress(job.progress);
if (job.status === 'completed' || job.status === 'failed') {
clearInterval(interval);
}
}, 2000); // Poll every 2 seconds
}
```
---
## Security
### Authentication (Future)
```typescript
// Include JWT in connection
const ws = new WebSocket(`ws://localhost:8000/api/v1/jobs/${jobId}/stream?token=${jwtToken}`);
```
### Rate Limiting
Limit connections per IP to prevent abuse:
```python
from slowapi import Limiter
@app.websocket("/api/v1/jobs/{job_id}/stream")
@limiter.limit("10/minute")
async def websocket_endpoint(...):
pass
```
---
## Next Steps
See [API Design](../backend/api.md) for REST endpoint integration.
|