Skip to content

WebSocket Chat

Overview

The WebSocket chat implementation provides real-time, bi-directional communication between users and the AI assistant through a web interface. It's currently the only active communication channel in the system.

Implementation Details

System Flow

```mermaid sequenceDiagram participant User participant Frontend participant Backend participant LLM Service participant Firestore

User->>Frontend: Connect to WebSocket
Frontend->>Backend: Send auth message
Backend->>Firestore: Verify user
Backend-->>Frontend: Accept connection
User->>Frontend: Send message
Frontend->>Backend: Forward message
Backend->>LLM Service: Generate response
LLM Service-->>Backend: Stream response
Backend-->>Frontend: Stream chunks
Frontend-->>User: Display response

```

Code Flow

```mermaid graph TD subgraph Frontend [DEPRECATED - Will be removed] A[ChatComponent] → B[WebSocket Connection] B → C[Message Handling] C → D[UI Updates] end

subgraph Backend
    E[WebSocket Endpoint] --> F[Auth Middleware]
    F --> G[verify_firebase_token]

    subgraph Application Layer
        G --> H[chatbot_websocket]
        H --> I[LLMService]
    end

    subgraph Domain Layer
        I --> J[Response Generation]
        J --> K[Streaming]
    end

    subgraph Infrastructure Layer
        K --> L[WebSocket]
        L --> M[Client]
    end
end

style Frontend fill:#f9f,stroke:#333,stroke-width:2px
style Backend fill:#bbf,stroke:#333,stroke-width:2px
style Application Layer fill:#dfd,stroke:#333,stroke-width:2px
style Domain Layer fill:#fdd,stroke:#333,stroke-width:2px
style Infrastructure Layer fill:#ddf,stroke:#333,stroke-width:2px

```

Components

Frontend [DEPRECATED - Will be removed]

  • ChatComponent.tsx: Main chat interface component
  • MessageList.tsx: Handles message display
  • ChatMessageItem.tsx: Individual message rendering

Backend

  • chatbot.py: WebSocket endpoint handler
  • llm_service.py: AI response generation
  • app_user_repository.py: User management

Features

  1. Real-time Communication
  2. Bi-directional WebSocket connection
  3. Streaming responses
  4. Instant message delivery

  5. Session Management

  6. Session ID generation
  7. Local storage persistence
  8. Automatic reconnection

  9. Message Handling

  10. Markdown formatting
  11. Code block support
  12. Error message display

  13. UI Features [DEPRECATED - Will be removed]

  14. Message history
  15. Connection status
  16. Expandable chat window
  17. Message timestamps

API Endpoints

  • GET /api/v1/async/chatbot: WebSocket connection endpoint

Error Handling

  1. Connection Errors
  2. Automatic reconnection
  3. User notification
  4. Error state display

  5. Authentication Errors

  6. Token validation
  7. Session verification
  8. Graceful disconnection

  9. Message Errors

  10. Error message display
  11. Retry mechanism
  12. State recovery

Monitoring

  1. Connection Status
  2. WebSocket state
  3. Connection duration
  4. Reconnection attempts

  5. Performance Metrics

  6. Response time
  7. Message throughput
  8. Error rates

Areas for Improvement

High Priority

  1. Frontend Migration
  2. Remove deprecated frontend components
  3. Update documentation
  4. Clean up unused code

  5. Session Management

  6. Implement server-side session storage
  7. Add session timeout handling
  8. Improve reconnection logic

  9. Error Handling

  10. Add specific error types
  11. Implement retry strategies
  12. Enhance error messages

Medium Priority

  1. Features
  2. Add message editing
  3. Implement message reactions
  4. Add file sharing

  5. Security

  6. Add message encryption
  7. Implement rate limiting
  8. Add IP blocking

  9. Performance

  10. Optimize message streaming
  11. Implement message batching
  12. Add rate limiting

Low Priority

  1. Documentation
  2. Add API documentation
  3. Create troubleshooting guide
  4. Document best practices

  5. Testing

  6. Add integration tests
  7. Implement load testing
  8. Add UI tests

  9. Maintenance

  10. Update dependencies
  11. Optimize code
  12. Clean up technical debt