eclipse-paho / eclipse-paho/paho.mqtt.java

How to implement a request-response pattern?

Open
#779 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2.3k
Forks
919
PR merge metrics
No merged PRs in 30d

Description

Hi,

I'm considering using this MQTT library for the Java implementation of the SDK for [emitter.io](https://github.com/emitter-io/emitter).

In some instances, the client has to talk to the server and expects an specific answer to a specific request. Typically when doing a key generation, a specific handler should be attached to the key generation request, that handle only the response to that one specific request. It feels like this lib has everything needed to do that. But I can't quite put it together.

I found I can retrieve a token via `deliveryComplete`, and that I can do a `setActionCallback` on this token. But first, I'm not entirely sure what an "action" means. Then, if it means what I think it means, how to get the actual response to my request from there?

```java
sampleClient.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
}

@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
System.out.println(topic);
System.out.println("setCallback: "+ message.toString());
}

@Override
public void deliveryComplete(IMqttDeliveryToken token) {
System.out.println(token.getMessageId());
token.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
var response = asyncActionToken.getResponse();

try {
System.out.println(new String(response.getPayload()));
} catch (MqttException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
}
});
}
});
```

Contributor guide

Open the contributing guide

Research direction

No source file or test is named. Start by reading the callback and token APIs referenced in the issue—MqttCallback, deliveryComplete, IMqttDeliveryToken, IMqttActionListener, and messageArrived—to determine whether request-response usage is supported and how responses are correlated. Done should be a clear, documented usage path or a precise explanation of the library's limitation.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, documentation
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.