Skip to main content

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

For production deployments with SSL:

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

  1. Client connects to WebSocket endpoint
  2. Client sends JSON request after connection opens
  3. Server streams text responses
  4. Server closes connection when complete
  5. 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:

Security Considerations

Authentication

For private repositories, include the access token in the request:

Connection Security

  1. Use WSS in Production: Always use wss:// (WebSocket Secure) in production
  2. Token Validation: Tokens are validated server-side before accessing repositories
  3. Origin Validation: Consider implementing origin checks for CORS security
  4. Rate Limiting: Implement connection rate limiting to prevent abuse

Example Security Headers

Complete Client Implementation Example

Performance Considerations

Advantages over HTTP Streaming

  1. Lower Latency: No HTTP overhead for each chunk
  2. Bidirectional: Enables future features like progress updates
  3. Connection Reuse: Single connection for entire session
  4. Binary Support: Can handle binary data if needed

Best Practices

  1. Implement Fallback: Always have HTTP streaming as fallback
  2. Handle Disconnects: Gracefully handle unexpected disconnections
  3. Buffer Management: Clear buffers after each completion
  4. Resource Cleanup: Close connections when component unmounts

Troubleshooting

Common Issues

  1. 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
  2. Immediate Disconnect
    • Verify request JSON format is valid
    • Check for missing required fields
    • Ensure messages array is not empty
  3. 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