Home » Android » android – can MQTT be used to implement Request / Response behavior

android – can MQTT be used to implement Request / Response behavior

Posted by: admin June 15, 2020 Leave a comment

Questions:

We are planning to use MQTT to deliver messages from our server to an android devices
we decided to go with the open source server mosquitto.

in most of the cases this is enough.(Publishing / subscribing )
but we have a cases where the client need to send parameters and get response from the server.
i know the direct approach is to go with Http (Servlets for example) .
but can we achieve this using MQTT since it would mean lower bandwidth consumption to the user ?

How to&Answers:

Yes, it just requires careful design of your topic structure. As a very simple example, you could publish to control/<client id>/request and subscribe to control/<client id>/response

Answer:

I recommend you look at how RabbitMQ handles RPC. Although AMQP is different than MQTT there is quite a bit of high level carry over.

It boils down to you either need the ability to create ephemeral “Reply To” queues with correlation ids using message headers. Or as @ralight said have two queues open for each client. The problem with two queues open for each client is of course ordering and thus you will have to use correlation ids and buffer and route internally (or drop till correlation id is found if your app is synchronous).

Answer:

As of MQTT version 5 this is possible. The specification has a Request/Response pattern specified.

Take a look at this: https://stackoverflow.com/a/59916330/1137669