Comments (5)
Hello, I have tried to reproduce the issue on the JVM but not on Android and I wasn't able to.
This is my sample code, can you try it please?
val client = MQTTClient(
5,
"test.mosquitto.org",
1883,
tls = null,
keepAlive = 30,
webSocket = false
) {
println(it.payload?.toByteArray()?.decodeToString())
}
// Subscribe to the first topic
client.subscribe(listOf(Subscription("/randomTopic1", SubscriptionOptions(Qos.EXACTLY_ONCE))))
thread {
// Publish every 5 seconds to both topics
while (true) {
Thread.sleep(5000)
client.publish(false, Qos.EXACTLY_ONCE, "/randomTopic1", "hello1".toByteArray().toUByteArray())
client.publish(false, Qos.EXACTLY_ONCE, "/randomTopic2", "hello2".toByteArray().toUByteArray())
}
}
thread {
// Wait 20 seconds before subscribing to the second topic, after run has been called
Thread.sleep(20000)
client.subscribe(listOf(Subscription("/randomTopic2", SubscriptionOptions(Qos.AT_LEAST_ONCE))))
}
client.run()
The output I'm getting is the following, as expected:
hello1
hello1
hello1
hello1
hello2
hello1
hello2
...
If it works also for you, please send me your sample code. Otherwise there might be something weird on Android and I will have to investigate that.
from kmqtt.
I see, I've got what the problem is.
I didn't know client.run()
blocks the thread, subscribe
wasn't invoked since it was right after it.
I know it's a bit off-topic here (maybe I can open another issue), but would it make sense to mark client.run()
as a suspend function? It's more intuitive for kotlin developers.
from kmqtt.
Yes, probably I should document that better.
I have been wanting to use coroutines in the project for a long time, but I haven't had the time unfortunately. Still it's one of the things I'd like to do in the future.
Regarding your specific question I could mark it with suspend and add a yield()
call in the loop to let other coroutines run.
For now, if you don't want to block a whole thread, you can call the non blocking client.step()
function periodically, which is basically what client.run()
does.
from kmqtt.
Yeah, thanks for the tips.
I'll keep an eye on the issues, in case you need a hand introducing coroutines/flows :)
from kmqtt.
I'll close this issue. Please open a new issue if you need specific APIs that you think might be helpful or if you have other design suggestions. Some help is always appreciated, thanks 😄
from kmqtt.
Related Issues (20)
- Can we use it in Android App as a background process? HOT 1
- Suggestions HOT 7
- Can we compile this code for android and iOS ? HOT 1
- HTTP header matching should be case insensitive to support more clients HOT 2
- Kotlin native could not find "openssl" HOT 8
- Can't download iOS dependencies (KMM) HOT 19
- Can't use client library for macos arm 64 HOT 4
- Recurring crash of Client on JVM HOT 1
- Seeking help HOT 1
- Unable to import MQTTClient in KMM project HOT 4
- connect to activemq HOT 6
- no default value for publishReceived
- callback for connect / disconnect & manage LWT HOT 2
- Artifact publication on Maven Central? HOT 3
- iOSBuild failed with Could not find 'openssl-its-simulator-arm64' HOT 2
- ssl server certificate not found (IOS only) HOT 4
- Failing to compile with Kotlin and Gradle KTS HOT 4
- "Unable to parse TLS packet header"-error caused by 0-byte-channel-read HOT 4
- Username and Password in the PacketInterceptor HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kmqtt.