一个im
{
"message_type": "forward",
"content": {
"forward_type": "merged",
"source_conversation_type": "group",
"source_conversation_name": "项目核心开发群",
"messages": [
{
"original_sender_name": "张三",
"timestamp": 1678886000000,
"content": {
"type": "text",
"body": "这是第一条被转发的消息。"
}
},
{
"original_sender_name": "李四",
"timestamp": 1678886100000,
"content": {
"type": "image",
"thumb_url": "https://...",
"width": 800,
"height": 600
}
},
{
"original_sender_name": "王五",
"timestamp": 1678886200000,
"content": {
"type": "text",
"body": "这是最后一条。"
}
}
],
"summary": "聊天记录 (3条)"
}
}
{
"message_type": "rich_text",
"content": {
"parts": [
{
"type": "text",
"body": "你好啊!下午我们去开会,别忘了 "
},
{
"type": "mention",
"user_id": "user_id_manager",
"display_text": "@张经理"
},
{
"type": "text",
"body": " 和 "
},
{
"type": "mention",
"mention_type": "all",
"display_text": "@全体成员"
}
],
"quote_message_seq": 98
}
}
services:
# 1. 用户服务
user-service:
implements:
- user.proto
- friend.proto # 好友关系和用户紧密相关
database: PostgreSQL
cache: Redis
responsibilities:
- 用户认证
- 用户信息管理
- 好友关系
- 黑名单
# 2. 消息服务
message-service:
implements:
- message.proto
- conversation.proto # 会话和消息紧密相关
database: MongoDB/Cassandra
cache: Redis
responsibilities:
- 消息发送/接收
- 消息存储
- 会话管理
- 未读计数
# 3. 群组服务
group-service:
implements:
- group.proto
database: PostgreSQL
cache: Redis
responsibilities:
- 群组管理
- 成员管理
- 群公告
# 4. 网关服务
gateway-service:
implements:
- gateway.proto
storage: Redis (会话状态)
responsibilities:
- WebSocket连接
- 消息推送
- 在线状态维护
# 5. 状态服务
presence-service:
implements:
- presence.proto
database: Redis
responsibilities:
- 在线状态
- 输入状态
- 已读回执
# 6. 搜索服务
search-service:
implements:
- search.proto
database: Elasticsearch
responsibilities:
- 全文搜索
- 搜索建议
# 7. 媒体服务
storage-service:
implements:
- storage.proto
storage: MinIO/S3
database: PostgreSQL (元数据)
responsibilities:
- 文件上传/下载
- 图片处理
- 视频转码
# 8. 通知服务
notification-service:
implements:
- notification.proto
database: PostgreSQL
queue: RabbitMQ/Kafka
responsibilities:
- 推送通知
- 邮件/短信
- 通知模板
直推扇出(小群)
• 找到在线成员连接,直接发 202-evt。
• 优点简单;100~200 人可行。
2. 订阅拉取(大群推荐)
• 服务端仅做「新消息信号」轻推(比如 202-evt 只含 session_id & max_seq);
• 客户端收到后按需调用 501 拉消息(客户端自适应节流/合并)。
• 可极大降低 WS 扇出压力。
3. 服务端批量聚合再推
• 网关侧为每个会话维护小批队列(时间窗 50~100ms / 条数阈值),合并后一次下发(N 条合并成 1 包)。
• 与 2)结合更佳。
im-server/
├── api/ # Fiber服务(HTTP + WebSocket)
│ ├── handler.go # 所有HTTP处理函数
│ ├── middleware.go # 认证中间件
│ ├── router.go # 路由配置
│ └── websocket.go # WebSocket处理
├── cmd/
│ └── api/
│ └── main.go # 主程序入口
├── internal/
│ ├── user/
│ │ └── user.go # 用户服务所有功能
│ ├── message/
│ │ └── message.go # 消息服务所有功能
│ ├── conversation/
│ │ └── conversation.go # 会话服务所有功能
│ ├── friend/
│ │ └── friend.go # 好友服务所有功能
│ ├── group/
│ │ └── group.go # 群组服务所有功能
│ └── common/
│ ├── db.go # 数据库
│ ├── redis.go # Redis
│ └── kafka.go # Kafka
├── proto/ # Proto文件目录
├── pb/ # 生成的pb文件
├── docker-compose.yml
├── Makefile
└── go.mod
{
"type": "message.read",
"req_id": "read_001",
"payload": {
"conversation_id": "c_single_userA_userB",
"read_seq": 105
}
}
{
"type": "message.send",
"payload": {
"client_msg_id": "uuid_v4_random_string_123",
"conversation_id": "single_01KC46E0D7X8BQXSRB29FJP960_01KC46F164MR4D6CWGR38A0GSG",
"msg_type": 1,
"content": "Hello via WebSocket!"
}
}
{
"type": "message.typing",
"req_id": "typing_001",
"payload": {
"conversation_id": "single_01KC46E0D7X8BQXSRB29FJP960_01KC46F164MR4D6CWGR38A0GSG"
}
}
{
"type": "message.edit",
"req_id": "edit_001",
"payload": {
"conversation_id": "single_01KC46E0D7X8BQXSRB29FJP960_01KC46F164MR4D6CWGR38A0GSG",
"message_id": "01KCHJM813FPB7A1KBZ40QV1HH",
"content": "Fixed content"
}
}
{
"type": "message.recall",
"req_id": "recall_001",
"payload": {
"conversation_id": "single_01KC46E0D7X8BQXSRB29FJP960_01KC46F164MR4D6CWGR38A0GSG",
"message_id": "01KCHJS21883W55RJF4XQE69KR"
}
}