Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/tabilzad/mqtt-kotlin-extensions

Provides Kotlin DSL for MQTT Paho client
https://github.com/tabilzad/mqtt-kotlin-extensions

dsl kotlin mqtt mqtt-client paho-mqtt-client

Last synced: 13 days ago
JSON representation

Provides Kotlin DSL for MQTT Paho client

Awesome Lists containing this project

README

        

# MQTT Kotlin Extensions

## Import
```kotlin
repositories {
mavenCentral()
}

implementation 'io.github.tabilzad:mqtt-kotlin-extensions:1.0'

```

## Connection Extensions

```kotlin

import java.util.UUIDimport

val client = MqttAsyncClient("tcp://mqtt.eclipseprojects.io:1883", UUID.randomUUID().toString())

client.orchestrate {
onConnectComplete { isReconnect, serverURI ->
log.info("Connected to $serverURI")
}
onMessageArrived { topic, message: MqttMessage ->
log.info("Message arrived on topic $topic")
log.info(message.toString())
}
onDeliveryComplete {
log.info("Message delivery of ${it.messageId} complete")
}
onConnectionLost { exception ->
log.debug("MQTT connection lost due to ${exception?.message}")
}
catchError { exception ->
log.error("Failed to connect to broker due to ${exception?.message}")
}
}
```

```kotlin
client.connect {
onSuccess {
log.info("MQTT Session started")
}
onFailure { _, exception ->
log.error("Failed to connect due to ${exception.message}")
}
}
```
## Action extensions
```kotlin
val topic = "my_topic"
client.subscribe(topic, 1) {
onSuccess {
log.info("Now subscribed to $topic successfully")
}
onFailure { _, t ->
log.error("Failed to subscribe to $topic due to: ${t?.message}")
}
}
```