Server-Sent Events endpoint for real-time updates
()
| 501 | |
| 502 | @app.route('/api/events') |
| 503 | def event_stream(): |
| 504 | """Server-Sent Events endpoint for real-time updates""" |
| 505 | def generate(): |
| 506 | try: |
| 507 | while True: |
| 508 | try: |
| 509 | # Wait for events with timeout |
| 510 | event = event_queue.get(timeout=30) |
| 511 | yield f"data: {json.dumps(event)}\n\n" |
| 512 | except queue.Empty: |
| 513 | # Send heartbeat to keep connection alive |
| 514 | yield f"data: {json.dumps({'type': 'heartbeat'})}\n\n" |
| 515 | except Exception as e: |
| 516 | yield f"data: {json.dumps({'type': 'error', 'message': str(e)})}\n\n" |
| 517 | break |
| 518 | except GeneratorExit: |
| 519 | pass |
| 520 | |
| 521 | return Response(generate(), mimetype='text/plain', headers={ |
| 522 | 'Cache-Control': 'no-cache', |
| 523 | 'Connection': 'keep-alive', |
| 524 | 'Access-Control-Allow-Origin': '*' |
| 525 | }) |
| 526 | |
| 527 | @app.route('/api/graph/stream', methods=['POST']) |
| 528 | def graph_stream(): |
nothing calls this directly
no test coverage detected