# zerodha/gokiteconnect architecture

> Architecture diagram of the GitHub repository zerodha/gokiteconnect, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/zerodha/gokiteconnect
- Repository: https://github.com/zerodha/gokiteconnect

## Overview

##### Main Components

a. Client Layer:
- Two main sub-components:
- REST Client (from connect.go, http.go)
- WebSocket Client (from ticker/ticker.go)

b. Feature Modules:
- User Management (user.go)
- Orders Management (orders.go)
- Portfolio Management (portfolio.go)
- Market Data (market.go)
- Margins (margins.go)
- Mutual Funds (mutualfunds.go)

c. Models Layer:
- A "Models" container (from models/ directory)
- Model types like Time and Snaps

d. External Systems:
- Kite Connect REST API
- Kite Connect WebSocket API
- Client Applications (users of this SDK)

##### Connection and Data Flow

- Bidirectional communication between the SDK and Kite Connect servers

##### Important Elements to Highlight

a. Authentication Flow:
- Login URL generation
- Session token generation
- Access token management

b. Real-time Data Flow:
- WebSocket connection
- Ticker subscription
- Event callbacks

##### Technical Details

- That it's a Go-based SDK
- Version compatibility (v4)
- HTTP/REST and WebSocket protocols
- Callback interfaces for real-time events

**[Client Applications] → [Kite Connect SDK] → [Kite Connect APIs]**

**[REST Client]     [WebSocket Client]**
- ↓                   ↓
**[Feature Modules] [Real-time Events]**
- ↓                   ↓
**[Models Layer]    [Error Handling]**

**[Authentication] [Configuration] [Utilities]**

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Client["Client Applications"]
        App[Client Apps]
    end

    subgraph SDK["Kite Connect SDK"]
        subgraph Clients["Client Layer"]
            REST["REST Client"]:::core
            WS["WebSocket Client"]:::core
        end

        subgraph Features["Feature Modules"]
            User["User Management"]:::module
            Orders["Orders Management"]:::module
            Portfolio["Portfolio Management"]:::module
            Market["Market Data"]:::module
            Margins["Margins"]:::module
            MF["Mutual Funds"]:::module
            GTT["GTT"]:::module
        end

        subgraph Models["Models Layer"]
            Time["Time Model"]:::model
            Snaps["Snaps Model"]:::model
        end

        subgraph Utils["Utilities"]
            Error["Error Handling"]:::util
            Tests["Test Suite"]:::util
            Examples["Examples"]:::util
            CICD["CI/CD Pipeline"]:::util
        end
    end

    subgraph External["External Systems"]
        KAPI["Kite Connect REST API"]:::external
        KWS["Kite Connect WebSocket API"]:::external
    end

    %% Connections
    App --> REST & WS
    REST --> KAPI
    WS -.-> KWS
    
    REST --> Features
    WS --> Features
    Features --> Models
    Features --> Utils

    %% Click Events
    click REST "https://github.com/zerodha/gokiteconnect/blob/master/connect.go"
    click WS "https://github.com/zerodha/gokiteconnect/blob/master/ticker/ticker.go"
    click User "https://github.com/zerodha/gokiteconnect/blob/master/user.go"
    click Orders "https://github.com/zerodha/gokiteconnect/blob/master/orders.go"
    click Portfolio "https://github.com/zerodha/gokiteconnect/blob/master/portfolio.go"
    click Market "https://github.com/zerodha/gokiteconnect/blob/master/market.go"
    click Margins "https://github.com/zerodha/gokiteconnect/blob/master/margins.go"
    click MF "https://github.com/zerodha/gokiteconnect/blob/master/mutualfunds.go"
    click GTT "https://github.com/zerodha/gokiteconnect/blob/master/gtt.go"
    click Time "https://github.com/zerodha/gokiteconnect/blob/master/models/time.go"
    click Snaps "https://github.com/zerodha/gokiteconnect/blob/master/models/snaps.go"
    click Error "https://github.com/zerodha/gokiteconnect/blob/master/errors.go"
    click Examples "https://github.com/zerodha/gokiteconnect/tree/master/examples"
    click Tests "https://github.com/zerodha/gokiteconnect/blob/master/connect_test.go"
    click CICD "https://github.com/zerodha/gokiteconnect/blob/master/.github/workflows/go-test.yml"

    %% Styling
    classDef core fill:#3498db,stroke:#2980b9,color:white
    classDef module fill:#e67e22,stroke:#d35400,color:white
    classDef model fill:#f1c40f,stroke:#f39c12,color:black
    classDef util fill:#95a5a6,stroke:#7f8c8d,color:white
    classDef external fill:#2ecc71,stroke:#27ae60,color:white

    %% Legend
    subgraph Legend
        L1["Core Components"]:::core
        L2["Feature Modules"]:::module
        L3["Data Models"]:::model
        L4["Utilities"]:::util
        L5["External Systems"]:::external
    end
```
