# app.py (Server) from fastapi import FastAPI, Request from fastapi.responses import StreamingResponse, HTMLResponse from fastapi.staticfiles import StaticFiles from fastapi.middleware.cors import CORSMiddleware import asyncio import time # Missing import causing NameError app = FastAPI() # CORS Configuration app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"], ) # Streamable HTTP Endpoint async def data_streamer(): """Generates real-time data chunks""" for i in range(1, 11): await asyncio.sleep(1) yield f"data: Chunk {i} - {time.ctime()}\n\n" @app.get("/stream") async def stream_data(): return StreamingResponse( data_streamer(), media_type="text/event-stream", headers={"Cache-Control": "no-cache"} ) # SPA Client Endpoint @app.get("/", response_class=HTMLResponse) async def get_spa(): return """ Stream Demo

Real-time Stream Demo

"""