基于 Golang Fiber 实现 AI 领域的 SSE 流式通信实践指南
1. 引言
在现代 AI 应用中,实时流式响应已成为标配。无论是 ChatGPT 这样的对话模型,还是实时语音转写等场景,都需要服务端能够持续地向客户端推送数据。Server-Sent Events (SSE) 技术凭借其简单高效的特点,成为这类场景的理想选择。本文将详细介绍如何使用 Golang Fiber 框架实现 AI 领域的 SSE 流式通信。
2. 技术栈选择说明
2.1 为什么选择 SSE 而不是 WebSocket?
- SSE 专注于服务器到客户端的单向通信,非常适合 AI 流式输出场景
- 实现简单,使用标准 HTTP 协议,无需额外的协议支持
- 自动重连机制,提供更好的容错能力
- 对服务器资源消耗更小
2.2 为什么选择 Fiber?
- 高性能的 Go Web 框架
- 内置优秀的流式处理支持
- API 设计简洁直观
- 良好的并发处理能力
3. 完整实现示例
3.1 服务端实现
首先创建基础的项目结构:
mkdir ai-sse-demo
cd ai-sse-demo
go mod init ai-sse-demo
创建主程序文件 main.go:
package main
import (
"encoding/json"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"time"
)
// AIResponse 表示AI响应的数据结构
type AIResponse struct {
Content string `json:"content"`
Done bool `json:"done"`
}
func main() {
app := fiber.New()
// 配置CORS
app.Use(cors.New(cors.Config{
AllowOrigins: "*",
AllowHeaders: "Origin, Content-Type, Accept",
}))
// 设置路由
app.Get("/", handleHome)
app.Get("/stream", handleSSE)
app.Listen(":3000")
}
// handleHome 返回测试页面
func handleHome(c *fiber.Ctx) error {
return c.SendFile("./static/index.html")
}
// handleSSE 处理SSE连接
func handleSSE(c *fiber.Ctx) error {
c.Set("Content-Type", "text/event-stream")
c.Set("Cache-Control", "no-cache")
c.Set("Connection", "keep-alive")
c.Set("Transfer-Encoding", "chunked")
// 创建通道用于模拟AI响应
responseChan := make(chan AIResponse)
// 启动模拟AI处理的goroutine
go simulateAIProcessing(responseChan)
// 发送SSE数据流
for response := range responseChan {
data, err := json.Marshal(response)
if err != nil {
continue
}
if _, err := c.Write([]byte("data: " + string(data) + "\n\n")); err != nil {
return err
}
c.Context().Flush()
if response.Done {
break
}
}
return nil
}
// simulateAIProcessing 模拟AI处理过程
func simulateAIProcessing(ch chan AIResponse) {
defer close(ch)
responses := []string{
"你好!我是AI助手",
"我正在处理你的请求",
"这是一个流式响应示例",
"马上就要完成了",
"处理完成!",
}
for i, text := range responses {
ch <- AIResponse{
Content: text,
Done: i == len(responses)-1,
}
time.Sleep(1 * time.Second)
}
}
3.2 前端实现
创建 static/index.html:
<!DOCTYPE html>
<html>
<head>
<title>AI Stream Demo</title>
<style>
#output {
width: 100%;
height: 300px;
border: 1px solid #ccc;
padding: 10px;
margin-top: 20px;
overflow-y: auto;
}
.message {
margin: 5px 0;
padding: 5px;
border-radius: 5px;
background-color: #f0f0f0;
}
</style>
</head>
<body>
<h1>AI Stream Demo</h1>
<button onclick="startStream()">开始流式响应</button>
<div id="output"></div>
<script>
function startStream() {
const output = document.getElementById('output');
output.innerHTML = ''; // 清空之前的内容
const eventSource = new EventSource('http://localhost:3000/stream');
eventSource.onmessage = function(event) {
const response = JSON.parse(event.data);
// 创建新的消息元素
const messageDiv = document.createElement('div');
messageDiv.className = 'message';
messageDiv.textContent = response.content;
output.appendChild(messageDiv);
// 自动滚动到底部
output.scrollTop = output.scrollHeight;
// 如果收到完成标志,关闭连接
if (response.done) {
eventSource.close();
}
};
eventSource.onerror = function(error) {
console.error('EventSource failed:', error);
eventSource.close();
};
}
</script>
</body>
</html>
3.3 运行和测试
- 安装依赖:
go mod tidy
- 运行服务:
go run main.go
- 访问 http://localhost:3000 进行测试
4. 代码详解
4.1 服务端关键点解析
- SSE 配置
c.Set("Content-Type", "text/event-stream")
c.Set("Cache-Control", "no-cache")
c.Set("Connection", "keep-alive")
这些头部设置确保了正确的 SSE 连接建立。
- 数据流处理
for response := range responseChan {
data, err := json.Marshal(response)
if err != nil {
continue
}
if _, err := c.Write([]byte("data: " + string(data) + "\n\n")); err != nil {
return err
}
c.Context().Flush()
}
使用通道来处理异步数据流,确保数据能够实时发送到客户端。
4.2 前端关键点解析
- EventSource 建立
const eventSource = new EventSource('http://localhost:3000/stream');
- 消息处理
eventSource.onmessage = function(event) {
const response = JSON.parse(event.data);
// 处理消息...
}
5. 性能优化建议
- 连接管理
- 实现超时机制
- 添加错误重试策略
- 控制并发连接数
- 内存管理
- 及时关闭不活跃的连接
- 使用对象池复用内存
- 控制消息大小
- 网络优化
- 启用 GZIP 压缩
- 实现断点续传
- 使用心跳保活
6. 实际应用场景
- AI 对话系统
- 实时返回 AI 模型的推理结果
- 支持打字机效果的渐进式显示
- 实时语音转写
- 将语音实时转换为文字
- 支持中间结果的实时展示
- 图像生成进度
- 展示 AI 绘画的实时进度
- 返回阶段性的预览图像
7. 总结
我们通过实际的示例,详细介绍了如何使用 Golang Fiber 实现基于 SSE 的 AI 流式响应系统。通过完整的代码示例和实践指南,展示了从服务端到前端的完整实现过程。SSE 技术在 AI 领域有着广泛的应用前景,希望本文能够帮助大家更好地理解和应用这项技术。
关注公众号,定期更新AI、Python、Golang领域内容