from fastapi import FastAPI, WebSocket
app = FastAPI()
@app.websocket("/data")
async def data_websocket(websocket: WebSocket):
await websocket.accept()
while True:
data = await generate_data() # Your function to get real-time data
await websocket.send_json(data)
await asyncio.sleep(1) # Adjust based on desired update frequency
Replace polling with web sockets e.g.