Code Monkey home page Code Monkey logo

Comments (5)

davidepianca98 avatar davidepianca98 commented on May 24, 2024

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.

paolorotolo avatar paolorotolo commented on May 24, 2024

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.

davidepianca98 avatar davidepianca98 commented on May 24, 2024

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.

paolorotolo avatar paolorotolo commented on May 24, 2024

Yeah, thanks for the tips.
I'll keep an eye on the issues, in case you need a hand introducing coroutines/flows :)

from kmqtt.

davidepianca98 avatar davidepianca98 commented on May 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.