【FastAPI】实现服务器向客户端发送SSE(Server-Sent Events)广播

在FastAPI中实现服务器向客户端发送SSE(Server-Sent Events)广播,可以通过以下步骤实现。SSE是一种服务器推送技术,允许服务器发送实时数据到客户端,通常用于创建实时更新的应用程序。

1. 安装必要的依赖

首先,确保你已经安装了FastAPI和Uvicorn:

bash 复制代码
pip install fastapi uvicorn

2. FastAPI服务器实现SSE广播

FastAPI使用StreamingResponse可以实现SSE。以下是一个实现SSE广播的简单例子,服务器会定期向所有连接的客户端广播消息。

python 复制代码
from fastapi import FastAPI, Request
from fastapi.responses import StreamingResponse
from typing import List
import asyncio

app = FastAPI()

# 用于保存所有的客户端连接
clients: List[asyncio.Queue] = []

# SSE生成器,用于向客户端发送事件流
async def event_generator():
    queue = asyncio.Queue()  # 每个客户端拥有自己的消息队列
    clients.append(queue)
    try:
        while True:
            data = await queue.get()  # 等待新消息
            yield f"data: {data}\n\n"
    except asyncio.CancelledError:
        clients.remove(queue)  # 客户端断开连接时移除其队列
        raise

# SSE endpoint,用于客户端连接
@app.get("/sse")
async def sse(request: Request):
    # 返回SSE流
    return StreamingResponse(event_generator(), media_type="text/event-stream")

# 广播消息到所有客户端
async def broadcast_message(message: str):
    for queue in clients:
        await queue.put(message)  # 将消息放入所有客户端的队列中

# 定期发送广播消息
@app.on_event("startup")
async def startup_event():
    async def send_messages():
        count = 0
        while True:
            await asyncio.sleep(5)  # 每5秒发送一次消息
            await broadcast_message(f"Server message: {count}")
            count += 1

    asyncio.create_task(send_messages())

3. 客户端实现

客户端可以使用EventSource对象来接收SSE事件流。以下是一个简单的HTML客户端代码:

html 复制代码
<!DOCTYPE html>
<html>
<head>
    <title>SSE Demo</title>
</head>
<body>
    <h1>Server-Sent Events</h1>
    <div id="messages"></div>

    <script>
        const eventSource = new EventSource("/sse");

        eventSource.onmessage = function(event) {
            const newMessage = document.createElement("div");
            newMessage.textContent = event.data;
            document.getElementById("messages").appendChild(newMessage);
        };

        eventSource.onerror = function() {
            console.log("Error occurred or connection closed.");
        };
    </script>
</body>
</html>

4. 运行FastAPI服务器

使用Uvicorn运行服务器:

bash 复制代码
uvicorn main:app --reload

这样,服务器会每5秒向所有连接的客户端广播消息。客户端只需要访问/sse即可建立与服务器的SSE连接,并实时接收服务器推送的消息。

关键点说明:

  • SSE(Server-Sent Events) :服务器通过text/event-stream向客户端推送数据,客户端通过EventSource接收。
  • 广播实现:将消息放入所有客户端的队列中,确保每个连接的客户端都能接收到消息。
  • StreamingResponse:用于持续向客户端发送数据流。

这样,你就可以在FastAPI中实现一个SSE广播功能了。

相关推荐
氵文大师15 分钟前
A机通过 python -m http.server 下载B机的文件
linux·开发语言·python·http
HUT_Tyne26520 分钟前
Linux 快速入门
linux·运维·服务器
程序员爱钓鱼24 分钟前
用 Python 批量生成炫酷扫光 GIF 动效
后端·python·trae
封奚泽优27 分钟前
下降算法(Python实现)
开发语言·python·算法
java1234_小锋32 分钟前
基于Python深度学习的车辆车牌识别系统(PyTorch2卷积神经网络CNN+OpenCV4实现)视频教程 - 自定义字符图片数据集
python·深度学习·cnn·车牌识别
爱笑的眼睛1139 分钟前
深入理解MongoDB PyMongo API:从基础到高级实战
java·人工智能·python·ai
鸠摩智首席音效师44 分钟前
如何在 Linux 中使用 dd 命令 ?
linux·运维·服务器
辣椒酱.1 小时前
jupyter相关
python·jupyter
郝学胜-神的一滴1 小时前
Python中常见的内置类型
开发语言·python·程序人生·个人开发
火白学安全1 小时前
《Python红队攻防零基础脚本编写:进阶篇(一)》
开发语言·python·安全·web安全·网络安全·系统安全