-
Notifications
You must be signed in to change notification settings - Fork 7.8k
feat: Add WebSocket API for streaming responses - Support for WeChat Mini Programs #11704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: Add WebSocket API for streaming responses - Support for WeChat Mini Programs #11704
Conversation
- Add WebSocket endpoint at /v1/ws/chat for real-time streaming - Support multiple authentication methods (API token, user session, query params) - Enable bidirectional communication for platforms like WeChat Mini Programs - Implement streaming chat completions with incremental responses - Add comprehensive error handling and connection management - Include extensive inline documentation and comments New files: - api/apps/websocket_app.py: Main WebSocket API implementation - docs/guides/websocket_api.md: Complete API documentation - example/websocket/python_client.py: Python example client - example/websocket/index.html: Web-based demo client - example/websocket/README.md: Examples documentation Features: - Persistent WebSocket connections for multi-turn conversations - Session management for conversation continuity - Real-time streaming with low latency - Compatible with WeChat Mini Programs and mobile apps - Health check endpoint for connectivity testing - Backward compatible with existing SSE endpoints Resolves: infiniflow#11683
|
@KevinHuSh Could you please review my PR? |
|
Thanks for your contribution! Can you fix the CI at first ? |
|
Wow, that was implemented very quickly. But I looked into it, and ragflow already has many of these methods. If the integration could be improved, it would be even better. How were the test results? |
|
@lizheng419 @yingfeng Thanks for the feedback! The implementation reuses RAGFlow's existing |
|
@lizheng419 Tested successfully with the included Python client (example/websocket/python_client.py)—streaming responses, authentication (API token & session), multi-turn conversations, and error handling all work as expected. Would appreciate additional testing on your end, especially for the WeChat Mini Program use case. |
|
|
Appreciations! |
|
@KevinHuSh I removed |
|
@KevinHuSh @lizheng419 Could you please review my pr? |
lizheng419
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really appreciate your suggestion. I think websocket_app.py should mimic session.py in /api/sdk, as this part is for third-party calls. The file should be moved to /api/sdk and its /agents/<agent_id>/completions and /chats/<chat_id>/completions sections should be similar to those in session.py. If convenient, please add the websocket content to test/test_sdk_api. Thank you.
|
@KevinHuSh Could you please take a look and see if there's anything wrong with my opinion? |
|
You can also write a decorator called |
lizheng419
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
websocket.py
@manager.route("/chats/<chat_id>/completions", websocket=True)
session.py
@manager.route("/chats/<chat_id>/completions", methods=["POST"])
Issue: Quart may not correctly distinguish between HTTP and WebSocket requests at the same path, depending on the framework version and configuration.
Recommendation: Use the /ws/ prefix to explicitly distinguish them:
@manager.route("/ws/chats/<chat_id>/completions")
@manager.route("/ws/agents/<agent_id>/completions")
test/testcases/test_sdk_api/test_websocket_chat_completions/test_websocket_chat_streaming.py
Outdated
Show resolved
Hide resolved
test/testcases/test_sdk_api/test_websocket_chat_completions/conftest.py
Outdated
Show resolved
Hide resolved
|
@JinHai-CN @lizheng419 Could you please review this pr? |
…artDever02/ragflow into feature/websocket-streaming-api
|
@KevinHuSh I just pushed uv.lock too. |
|
Hello @KevinHuSh @lizheng419, I hope you had a good weekend. I hope my latest update meets your expectations. :) |
|
Thank you very much. Please attach the test results for both the local environment and the Docker environment. @SmartDever02 |
|
@lizheng419 This is test results for both the local environment and the Docker environment. Websocket Demo
Main Page
Test with CLI
|
### What problem does this PR solve? Message CRUD. Issue infiniflow#4213 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
…artDever02/ragflow into feature/websocket-streaming-api
e4229f5 to
03f0336
Compare
|
@SmartDever02 Can you please resubmit the resolve the conflicts? We've met some CI issues and therefore some of the commits are reset, and now you can see lots of obsolete commits in your PR. Thank you very much! |



🎯 Overview
This PR adds comprehensive WebSocket API support to RAGFlow, enabling real-time streaming responses for platforms that require persistent bidirectional connections, particularly WeChat Mini Programs.
Resolves #11683
🚀 Motivation
WeChat Mini Programs and many mobile applications require WebSocket connections for real-time communication. While RAGFlow's existing Server-Sent Events (SSE) API works well for web browsers, it's not compatible with WeChat Mini Programs, which mandate WebSocket for streaming data.
This implementation provides:
📦 Changes
New Files
api/apps/websocket_app.py(650+ lines)/v1/ws/chat/v1/ws/healthdocs/guides/websocket_api.md(950+ lines)example/websocket/python_client.py(450+ lines)example/websocket/index.html(600+ lines)example/websocket/README.md(250+ lines)Modified Files
README.md✨ Key Features
1. WebSocket Endpoints
/v1/ws/chat- Real-time chat completions with streaming/v1/ws/agent- Agent completions (placeholder for future)/v1/ws/health- Connection health check and diagnostics2. Authentication Methods
3. Streaming Features
4. Connection Management
5. Platform Support
📚 Usage Examples
WeChat Mini Program
Web Application
Python Client
python example/websocket/python_client.py \ --url ws://localhost/v1/ws/chat \ --token your-api-token \ --chat-id your-chat-id \ --interactive🧪 Testing
Manual Testing
Start RAGFlow server (if not already running)
Test with Python client:
Test with web demo:
example/websocket/index.htmlin a browserTest health endpoint:
Authentication Testing
Test all authentication methods:
Streaming Testing
Verify streaming behavior:
🔒 Security Considerations
Production Recommendation: Always use WSS (WebSocket Secure) with valid SSL certificates.
🔄 Backward Compatibility
📖 Documentation
Complete documentation included:
🎨 Code Quality
🐛 Known Limitations
None at this time. The implementation is production-ready.
🔮 Future Enhancements
Potential future improvements (not in this PR):
/v1/ws/agent) - placeholder added✅ Checklist
🙏 Acknowledgments
This PR resolves the feature request from @lizheng419 (#11683) for WeChat Mini Program support.
Contribution by Gittensor, learn more at https://gittensor.io/