sequenceDiagram
    participant U1 as User 1
    participant U2 as User 2
    participant F1 as Frontend 1
    participant F2 as Frontend 2
    participant A as API Gateway
    participant C as Chat Server
    participant DB as PostgreSQL
    participant R as Redis

    Note over U1,R: Real-time Chat Flow

    U1->>F1: Open chat interface
    F1->>A: Request chat rooms
    A->>C: GET /api/rooms
    C->>DB: Query user rooms
    DB->>C: Return room list
    C->>F1: Return rooms

    U1->>F1: Select room
    F1->>C: WebSocket connection
    C->>R: Register user session
    C->>F1: Connection established

    U1->>F1: Type message
    F1->>C: WebSocket: SendMessage
    C->>DB: Store message
    DB->>C: Message stored
    C->>R: Update room cache
    C->>F1: Message confirmation
    C->>F2: Broadcast to other users

    U2->>F2: Receive message
    F2->>U2: Display message

    Note over U1,U2: Message Reactions

    U2->>F2: React to message
    F2->>C: WebSocket: AddReaction
    C->>DB: Store reaction
    C->>R: Update cache
    C->>F1: Broadcast reaction
    C->>F2: Reaction confirmation

    Note over U1,U2: Typing Indicators

    U1->>F1: Start typing
    F1->>C: WebSocket: TypingStart
    C->>F2: Broadcast typing indicator
    U1->>F1: Stop typing
    F1->>C: WebSocket: TypingStop
    C->>F2: Remove typing indicator

    Note over U1,U2: Presence Management

    U1->>F1: Go offline
    F1->>C: WebSocket: Disconnect
    C->>R: Remove user session
    C->>F2: Broadcast user offline
    C->>DB: Update last_seen
