Answer the question
In order to leave comments, you need to log in
Paho mqtt, how to implement receiving from a broker?
I wrote a program that sends messages from the client to the broker, but I need to write a program that will receive messages from the broker.
An example of sending a message is below:
public class MqttPublishSample {
private String topic = "Lesson_MQTT_server";
private String content = "Lesson message";
private String clientId = "random id";
private String broker = "tcp://m15.cloudmqtt.com:18594";
private char [] password = "******".toCharArray();
private String userName = "*****";
private int qos = 2; // Max quality
private MemoryPersistence persistence;// wtf???
private void MqttMessage (String message) {
System.out.println (message);
}
public MqttPublishSample () {
try {
persistence = new MemoryPersistence ();
MqttMessage ("Create client");
MqttClient client = new MqttClient (broker, clientId, persistence);
MqttMessage ("Create setting of connection");
MqttConnectOptions options = new MqttConnectOptions ();
options.setCleanSession(true);
options.setUserName(userName);
options.setPassword(password);
MqttMessage ("Connect to server");
client.connect(options);
if (client.isConnected())
MqttMessage ("Connect is succeful!");
else
MqttMessage ("Connect falls");
MqttMessage("Publish message");
MqttMessage message = new MqttMessage (content.getBytes());
message.setQos(qos);
client.publish(topic, message);
MqttMessage("Message was published!");
client.disconnect();
MqttMessage("Disconnect!");
} catch (MqttException ex) {
System.out.println("reason: " + ex.getReasonCode());
System.out.println("msg: " + ex.getMessage());
System.out.println("loc : " + ex.getLocalizedMessage());
System.out.println("cause: " + ex.getCause());
}
}
public static void main (String...strings) {
new MqttPublishSample ();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question