KubeMQ
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:

Connect.kt
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()
}
BuilderUse For
KubeMQClient.pubSub { }Events, Events Store
KubeMQClient.queues { }Queues
KubeMQClient.cq { }Commands, Queries (RPC)

Send Your First Event

SendEvent.kt
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:

ReceiveEvents.kt
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

ParameterTypeDefaultDescription
addressStringlocalhost:50000KubeMQ server gRPC address
clientIdStringAuto-generatedUnique client identifier
authTokenString""JWT authentication token
logLevelLogLevelINFOLogging verbosity
keepAliveBooleantrueEnable gRPC keep-alive
pingIntervalSecondsInt10Keep-alive ping interval
pingTimeoutSecondsInt5Keep-alive ping timeout
maxReceiveSizeInt104857600Max inbound message size (100 MB)
reconnection { }DSL blockEnabledAuto-reconnection with backoff config
tls { }DSL blockDisabledTLS/mTLS configuration

Error Handling

The SDK uses a sealed exception hierarchy rooted at KubeMQException:

ErrorHandling.kt
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}")
    }
}
ExceptionRetryableWhen
KubeMQException.ConnectionYesServer unavailable
KubeMQException.TimeoutYesDeadline exceeded
KubeMQException.AuthenticationNoInvalid credentials
KubeMQException.AuthorizationNoInsufficient permissions
KubeMQException.ValidationNoInvalid parameters

Next Steps

Was this page helpful?

On this page