Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/tabilzad/mqtt-kotlin-extensions
- Owner: tabilzad
- License: apache-2.0
- Created: 2022-05-16T18:05:22.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-05-17T18:50:23.000Z (over 2 years ago)
- Last Synced: 2024-10-06T05:22:57.204Z (about 1 month ago)
- Topics: dsl, kotlin, mqtt, mqtt-client, paho-mqtt-client
- Language: Kotlin
- Homepage:
- Size: 65.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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}")
}
}
```