DitchOoM / mqtt

Kotlin cross-platform, coroutine based, reflectionless MQTT 3.1.1 & 5.0 client
27 stars 0 forks source link

Issue on getting the subscription packets #6

Closed Roger14 closed 1 year ago

Roger14 commented 1 year ago

Hi, thanks for sharing this amazing code. I have 2 questions. Appreciate you can assist.

1) Can you provide some sample codes on how the client can receive the published messages from MQTT broker? Based on the readme, below is the subscription codes.

val client = service.addBrokerAndStartClient(connections, connectionRequest)
val subscribeOperation = client.subscribe("test/+", QualityOfService.AT_LEAST_ONCE)

How should we receive the "test/+" topic messages? I was expecting to implement an onMessage method... :)

2) I like to clarify when we create the broker connection using the following way. when the Android app is in the background, with the service suppose to maintain and perform reconnection to ensure the connection between MQTT client and broker is still established? Because I realized when the app is in the background for some time, the connection will be closed. Any advice on how to implement a solution such that the app (in the background) is still able to publish messages to backend MQTT broker server?

val client = service.addBrokerAndStartClient(connections, connectionRequest)

Thanks a lot.

RK

thebehera commented 1 year ago

Good questions, I need to make this clear when I update the readme next.

Can you provide some sample codes on how the client can receive the published messages from MQTT broker?

As of now, there are two options you have:

If you want to go down this route, a you can combine the Flow like this:

combine(subscribe("test/+", qos).subscriptions.values) { pubs:Array<IPublishMessage> ->

}

when the Android app is in the background, with the service suppose to maintain and perform reconnection to ensure the connection between MQTT client and broker is still established?

Yes, however this is based on the background limitations that the Android OS has in place. This library does nothing to extend background execution, that is up you, the app developer to put those in place.

when the app is in the background for some time, the connection will be closed. Any advice on how to implement a solution such that the app (in the background) is still able to publish messages to backend MQTT broker server?

There are different ways to do this but it depends on your use case. For most solutions I would recommend letting the Android OS suspend or kill the service knowing that the mqtt service will automatically startup when the application runs again. If that doesn't fit your needs then I would consider looking into these options with their own pros and cons:

thebehera commented 1 year ago

closing for now, feel free to reopen if there are any other questions

Roger14 commented 1 year ago

Hi,

Thanks for the quick answers. Appreciate it.

I followed your recommendations and tried to publish and subscribe to the same topic.
In addition, I deployed another standalone MQTT client connecting to the same broker, to listen to the topic.
Below are the key codes:

client.subscribe("test/a", QualityOfService.AT_LEAST_ONCE)

client.observe(Topic.fromOrThrow("test/+", Topic.Type.Filter)).collect{
       println("$it.topic | $it.payload")
}
val payloadBuffer = PlatformBuffer.allocate(payload.length, AllocationZone.SharedMemory)
payloadBuffer.writeString("abc") 
client.publish("test/a", QualityOfService.EXACTLY_ONCE, payloadBuffer, false)

The result was that after I published using the above codes, the standalone MQTT client was able to receive the message. However, the above observe flow didn't get called and didn't receive any message.

May I know is there something which I did wrongly? Thanks for your time, once again. :)