# Client (/sdks/kotlin/reference/client)



The Kotlin SDK splits messaging APIs across focused clients: pub/sub (`PubSubClient`), queues (`QueuesClient`), and commands/queries (`CQClient`). All are created via `KubeMQClient` DSL builders and implement `Closeable`.

## Package [#package]

```kotlin title="build.gradle.kts"
implementation("io.kubemq.sdk:kubemq-sdk-kotlin:1.0.1")
```

Requires Kotlin 2.0+ and JDK 11+.

## Client classes [#client-classes]

| Builder                   | Returns        | Patterns             |
| ------------------------- | -------------- | -------------------- |
| `KubeMQClient.pubSub { }` | `PubSubClient` | Events, Events Store |
| `KubeMQClient.queues { }` | `QueuesClient` | Queues               |
| `KubeMQClient.cq { }`     | `CQClient`     | Commands, Queries    |

### DSL construction [#dsl-construction]

```kotlin
val pub = KubeMQClient.pubSub {
    address = "localhost:50000"
    clientId = "kotlin-pub"
}

val queues = KubeMQClient.queues {
    address = "localhost:50000"
    clientId = "kotlin-q"
}

val cq = KubeMQClient.cq {
    address = "localhost:50000"
    clientId = "kotlin-cq"
}
```

**Configuration properties:**

| Name                       | Type       | Required | Description                                      |
| -------------------------- | ---------- | -------- | ------------------------------------------------ |
| `address`                  | `String`   | Yes      | `host:port` for the broker                       |
| `clientId`                 | `String`   | Yes      | Stable ID for this process                       |
| `authToken`                | `String`   | No       | Bearer token                                     |
| `logLevel`                 | `LogLevel` | No       | Logging level (`DEBUG`, `INFO`, `WARN`, `ERROR`) |
| `keepAlive`                | `Boolean`  | No       | Enable gRPC keep-alive                           |
| `pingIntervalSeconds`      | `Int`      | No       | Keep-alive ping interval                         |
| `pingTimeoutSeconds`       | `Int`      | No       | Keep-alive ping timeout                          |
| `maxReceiveSize`           | `Int`      | No       | Max inbound message size                         |
| `ensureConnectedTimeoutMs` | `Long`     | No       | Connection timeout                               |
| `unaryTimeoutMs`           | `Long`     | No       | Timeout for unary gRPC calls                     |
| `tls { }`                  | DSL block  | No       | TLS/mTLS configuration                           |
| `reconnection { }`         | DSL block  | No       | Reconnection backoff tuning                      |

### Reconnection DSL [#reconnection-dsl]

```kotlin
reconnection {
    initialBackoffMs = 1000
    maxBackoffMs = 30_000
    multiplier = 2.0
    maxRetries = 10
}
```

### TLS DSL [#tls-dsl]

```kotlin
tls {
    caCertFile = "/path/to/ca.pem"
    certFile = "/path/to/client.pem"
    keyFile = "/path/to/client.key"
}
```

## Lifecycle [#lifecycle]

Use Kotlin's `use { }` block (equivalent to Java try-with-resources) because each client implements `Closeable`:

```kotlin
KubeMQClient.pubSub {
    address = "localhost:50000"
    clientId = "demo"
}.use { client ->
    // use client
}
```

## Connection state monitoring [#connection-state-monitoring]

```kotlin
client.connectionState.collect { state ->
    when (state) {
        is ConnectionState.Idle -> println("Idle")
        is ConnectionState.Connecting -> println("Connecting")
        is ConnectionState.Ready -> println("Ready")
        is ConnectionState.Reconnecting -> println("Reconnecting #${state.attempt}")
        is ConnectionState.Closed -> println("Closed")
    }
}
```

## Environment configuration [#environment-configuration]

```kotlin
val config = ClientConfig.fromEnvironment()
// Reads KUBEMQ_ADDRESS, KUBEMQ_CLIENT_ID, KUBEMQ_AUTH_TOKEN
```

## Quick Usage [#quick-usage]

```kotlin title="App.kt"
KubeMQClient.pubSub {
    address = "localhost:50000"
    clientId = "reference-demo"
}.use { pub ->
    // Events APIs live on PubSubClient -- see Events reference page
}
```

## See Also [#see-also]

* [Connection Examples](/sdks/kotlin/how-to/connection/)
* Concepts
* [Kotlin SDK Getting Started](/sdks/kotlin)
