File size: 752 Bytes
0ad74ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, WebSocket
from fastapi.responses import StreamingResponse
import asyncio

app = FastAPI()

async def number_generator():
    for number in range(1, 501):
        message = "Lorem "*(number + 1)
        yield f"data: {message}\n\n"
        await asyncio.sleep(0.01)

@app.get("/sse")
async def sse():
    return StreamingResponse(number_generator(), media_type="text/event-stream")

@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    for number in range(1, 501):
        message = "Lorem "*(number + 1)
        await websocket.send_text(message)
        await asyncio.sleep(0.01)
    await websocket.close()


import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)