WebSocket API
DeepWiki uses WebSocket connections to provide real-time streaming responses for chat completions and wiki generation. This enables a more responsive user experience with lower latency compared to traditional HTTP streaming.Overview
The WebSocket API replaces HTTP streaming endpoints with persistent WebSocket connections that:- Stream AI responses in real-time as they’re generated
- Provide immediate feedback for long-running operations
- Support bidirectional communication for future enhancements
- Automatically fall back to HTTP if WebSocket connection fails
Connection Establishment
Endpoint
Client Connection Example
Message Formats
Request Format
After establishing the connection, send a JSON message with the following structure:Response Format
The server streams text responses as they’re generated:Event Types
Connection Events
Message Flow
- Client connects to WebSocket endpoint
- Client sends JSON request after connection opens
- Server streams text responses
- Server closes connection when complete
- Client handles close event
Streaming Features
Wiki Generation
For wiki page generation, the WebSocket streams Markdown content in real-time:Chat/Ask Feature
The Ask feature uses WebSocket for real-time streaming with support for:- Conversation History: Maintains context across multiple questions
- Deep Research Mode: Multi-turn research with automatic continuation
- File Context: Include specific file content in queries
Connection Lifecycle
Automatic Closure
The server automatically closes the WebSocket connection after:- Completing the response stream
- Encountering an error
- Detecting client disconnect
Reconnection Strategy
Error Handling
Server-Side Errors
The server sends error messages as text before closing the connection:Client-Side Error Handling
Status Codes
WebSocket connections use standard close codes:Code | Status | Description |
---|---|---|
1000 | Normal Closure | Request completed successfully |
1001 | Going Away | Server is shutting down |
1002 | Protocol Error | Invalid message format |
1003 | Unsupported Data | Invalid request data |
1006 | Abnormal Closure | Connection lost unexpectedly |
1011 | Internal Error | Server encountered an error |
Security Considerations
Authentication
For private repositories, include the access token in the request:Connection Security
- Use WSS in Production: Always use
wss://
(WebSocket Secure) in production - Token Validation: Tokens are validated server-side before accessing repositories
- Origin Validation: Consider implementing origin checks for CORS security
- Rate Limiting: Implement connection rate limiting to prevent abuse
Example Security Headers
Complete Client Implementation Example
Performance Considerations
Advantages over HTTP Streaming
- Lower Latency: No HTTP overhead for each chunk
- Bidirectional: Enables future features like progress updates
- Connection Reuse: Single connection for entire session
- Binary Support: Can handle binary data if needed
Best Practices
- Implement Fallback: Always have HTTP streaming as fallback
- Handle Disconnects: Gracefully handle unexpected disconnections
- Buffer Management: Clear buffers after each completion
- Resource Cleanup: Close connections when component unmounts
Troubleshooting
Common Issues
-
Connection Refused
- Check if the API server is running on port 8001
- Verify WebSocket endpoint URL is correct
- Check for proxy/firewall blocking WebSocket connections
-
Immediate Disconnect
- Verify request JSON format is valid
- Check for missing required fields
- Ensure messages array is not empty
-
No Response
- Check server logs for errors
- Verify model provider credentials are set
- Ensure repository URL is accessible
Debug Logging
Enable detailed logging for troubleshooting:Future Enhancements
The WebSocket infrastructure enables future real-time features:- Progress indicators for long operations
- Cancel/interrupt ongoing generation
- Real-time collaboration features
- Live repository updates
- Streaming file analysis
- Interactive debugging sessions