Client SDKsKotlinTutorials
Send Your First Message
Connect the Kotlin client to KubeMQ and publish and receive your first message end to end.
This is your first hands-on lesson with the Kotlin SDK: create a client, send an event, and receive it. Make sure you have the SDK installed (see the Kotlin SDK overview).
Create a Client
The Kotlin SDK provides pattern-specific clients created via DSL builders:
import io.kubemq.sdk.client.KubeMQClient
import kotlinx.coroutines.runBlocking
fun main() = runBlocking {
val client = KubeMQClient.pubSub {
address = "localhost:50000"
clientId = "my-service"
}
val info = client.ping()
println("Connected to ${info.host} v${info.version}")
client.close()
}| Builder | Use For |
|---|---|
KubeMQClient.pubSub { } | Events, Events Store |
KubeMQClient.queues { } | Queues |
KubeMQClient.cq { } | Commands, Queries (RPC) |
Send Your First Event
import io.kubemq.sdk.client.KubeMQClient
import io.kubemq.sdk.pubsub.eventMessage
import kotlinx.coroutines.runBlocking
fun main() = runBlocking {
val client = KubeMQClient.pubSub {
address = "localhost:50000"
clientId = "events-sender"
}
client.use {
client.publishEvent(eventMessage {
channel = "notifications"
body = "hello kubemq".toByteArray()
metadata = "greeting"
})
println("Event sent")
}
}Receive Events
The Kotlin SDK uses Flow for subscriptions, providing natural integration with coroutines:
import io.kubemq.sdk.client.KubeMQClient
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.runBlocking
fun main() = runBlocking {
val client = KubeMQClient.pubSub {
address = "localhost:50000"
clientId = "events-receiver"
}
client.use {
client.subscribeToEvents {
channel = "notifications"
}.take(10).collect { event ->
println("Received: ${String(event.body)}")
}
}
}Configuration Options
| Parameter | Type | Default | Description |
|---|---|---|---|
address | String | localhost:50000 | KubeMQ server gRPC address |
clientId | String | Auto-generated | Unique client identifier |
authToken | String | "" | JWT authentication token |
logLevel | LogLevel | INFO | Logging verbosity |
keepAlive | Boolean | true | Enable gRPC keep-alive |
pingIntervalSeconds | Int | 10 | Keep-alive ping interval |
pingTimeoutSeconds | Int | 5 | Keep-alive ping timeout |
maxReceiveSize | Int | 104857600 | Max inbound message size (100 MB) |
reconnection { } | DSL block | Enabled | Auto-reconnection with backoff config |
tls { } | DSL block | Disabled | TLS/mTLS configuration |
Error Handling
The SDK uses a sealed exception hierarchy rooted at KubeMQException:
import io.kubemq.sdk.exception.KubeMQException
try {
client.publishEvent(message)
} catch (e: KubeMQException) {
when (e) {
is KubeMQException.Connection ->
println("Connection failed (retryable): ${e.message}")
is KubeMQException.Authentication ->
println("Auth failed: ${e.message}")
is KubeMQException.Validation ->
println("Invalid request: ${e.message}")
is KubeMQException.Timeout ->
println("Timeout (retryable): ${e.message}")
else ->
println("SDK error: ${e.message}")
}
}| Exception | Retryable | When |
|---|---|---|
KubeMQException.Connection | Yes | Server unavailable |
KubeMQException.Timeout | Yes | Deadline exceeded |
KubeMQException.Authentication | No | Invalid credentials |
KubeMQException.Authorization | No | Insufficient permissions |
KubeMQException.Validation | No | Invalid parameters |
Next Steps
- Kotlin SDK Reference -- full API documentation
- Kotlin SDK Examples -- complete examples for all patterns
- GitHub Repository -- source code and issues
Was this page helpful?