0

I'm using paho to subscribe to the mqtt topic "$SYS/#", in order to see client's connections.

I implemented the MqttCallback interface to receive message send in this topic, and for the moment I just want to display the content of message in the console.

My problem is that the message is received endlessly, the "messageArrived" function is call each second with always the same client name.

import android.app.Activity;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class MainActivity extends Activity {
    private static final String MQTT_DIR = "/home/persistence.log";
    private static final String MQTT_URI = "tcp://x.x.x.x:1883";
    private MqttClient client;

    private String clientId = "AndroidClient";
    private String clientName = "usermqtt";
    private char[] password = {'x', 'x', 'x'};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MemoryPersistence persistence = new MemoryPersistence();
        MqttConnectOptions options = new MqttConnectOptions();

        options.setUserName(clientName);
        options.setPassword(password);

        try {
            client = new MqttClient(MQTT_URI, clientId, persistence);
            client.connect(options);
            client.subscribe("$SYS/#");
        } catch (MqttException e) {
            e.printStackTrace();
        }

        client.setCallback(new MqttCallback() {
            @Override
            public void connectionLost(Throwable throwable) {
            }

            @Override
            public void messageArrived(String arg0, MqttMessage arg1)  
            throws Exception {
                // TODO Auto-generated method stub
                System.out.println("Message Received");
                System.out.println(arg0);
                System.out.println(arg1.toString());
            }

            @Override
            public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
            }
        });
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
igor
  • 495
  • 1
  • 4
  • 16
  • MQTT brokers are free to use "$SYS/" topics for special purposes such as sending client data information but the behaviour will differ from broker to broker because it is an application specific detail rather than a protocol specific. Which MQTT broker are you using? Is it possible that your MQTT broker publishes the client connection details every second? – Pierre-Luc Aug 30 '15 at 13:07
  • On server side, I use mosca (node.js broker). The broker publish only once, client connection details. I have the problem only with Android Client, with client in node or python it works like expected. – igor Aug 31 '15 at 09:42

0 Answers0