Skip to content

hemma/kmqtt

Repository files navigation

KMqtt

KMqtt is a kotlin library for communicating over mqtt, built upon paho mqtt.

Installation

Gradle

implementation 'dev.bothin.kmqtt:kmqtt:0.1.0'

Usage

See,

example

val mqttClient = MqttClient("tcp://localhost:1883", "clientId", MemoryPersistence())
val kMqttClient = KMqttClient(mqttClient)
kMqttClient.connect()

val kMqtt = KMqtt(kMqttClient)

val app = KMqttApplication(kMqttClient)
app {
    subscribe<Person>("hello/1") {
        println("Hello ${it.payload.name}")
        OutMessage.nothing()
    }

    subscribe<Person>("hello/+", "hello/response") {
        println("Hello + ${it.payload.name}")
        OutMessage(payload = it.payload.copy(name = "Kmqtt"), topicSuffix = "id")
    }

    subscribe<Any>("#") {
        println("Log: ${it.payload} on ${it.topic}")
        OutMessage.nothing()
    }
}

kMqtt.emit("hello/1", Person("Stefan"))

runBlocking {
    println("WaitReceive ${kMqtt.waitReceive<Person>("hello/response/+")}")

    println("EmitWaitReceive: ${kMqtt.emitWaitReceive("self", Person("Who?"), "self")}")
}


runBlocking {
    delay(1000)
    kMqttClient.unsubscribeAll()
    kMqttClient.disconnect()
}

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages