-
I'm supplying the reply-to in the request, but when the replier responds, it never gets back to the requestor. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
As shown in the code below, the NATS client cannot receive Reply messages from the MQTT client in this case: public static void main(String[] args) throws IOException, InterruptedException {
Options options = new Options.Builder()
.server("nats://192.168.2.102:14222")
.userInfo("seeiner", "seeiner")
.build();
Connection nc = Nats.connect(options);
Message message = NatsMessage.builder().replyTo("replyto.123")
.data("hello".getBytes(StandardCharsets.UTF_8))
.subject("request.123")
.build();
Message res = nc.request(message, Duration.ofSeconds(10));
System.out.println(new String(res.getData()));
} public static void main(String[] args) {
String content = "Message from MQTT";
MemoryPersistence persistence = new MemoryPersistence();
try {
MqttClient sampleClient = new MqttClient("tcp://192.168.2.102:11883", "sample_mqtt_client",
persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
connOpts.setUserName("seeinerx");
connOpts.setPassword("seeinerx".toCharArray());
sampleClient.connect(connOpts);
sampleClient.subscribe("request/123", 0, (s, mqttMessage) -> {
System.out.println("Received mesage from nats: " + new String(mqttMessage.getPayload()));
System.out.println("Publishing message: " + content);
MqttMessage message = new MqttMessage(content.getBytes());
message.setQos(0);
sampleClient.publish("replyto/123", message);
System.out.println("Message published");
});
Thread.sleep(30000L);
sampleClient.disconnect();
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
}
} |
Beta Was this translation helpful? Give feedback.
-
The problem is that you are replying to the wrong subject. When you make the request in java, your If you can't access the reply-to field on the mqtt side, then what I would do is used fixed subjects, have java just publish (not request) to Also, in the case of a publish, the client will not provide it's own reply-to so you could use that to provide your own subject. You can also use headers to supply information. |
Beta Was this translation helpful? Give feedback.
The problem is that you are replying to the wrong subject. When you make the request in java, your
replyTo("replyto.123")
is ignored, the client makes a reply to that looks like this_INBOX.vqNUxbuYpUUHepWvptfH5M.vqNUxbuYpUUHepWvptfH8c
On the mqtt side, you must look at the exact replyTo that is with the message you receive and publish to it.
If you can't access the reply-to field on the mqtt side, then what I would do is used fixed subjects, have java just publish (not request) to
request.123
and then be a subscriber to fixed subjectreply.123
Also, in the case of a publish, the client will not provide it's own reply-to so you could use that to provide your own subject. You can also use …