0
votes

Im trying to use Eclipse Paho MQTT client on my Android app to receive MQTT messages.

Im receiving ~100 message/second, messages are fairly small (basically small json objects). These messages are coming from Mosquitto broker.

When I subscribe to MQTT topic, and start to receive messages, my Android app UI almost freezes. I do not even do anything about the received message on messageArrived callback.

My Eclipse Paho MQTT client usage is super basic, almost the same as in Eclipse Paho example.

Is Eclipse Paho MQTT client bad on performance, or is there something I might be doing wrong?

Im using: org.eclipse.paho.client.mqttv3:1.1.0 & org.eclipse.paho.android.service:1.1.1

Update

Below my actual code, which is part of the fragment. So in this test im just logging the received message topic. Also with out logging, I got same result, my app almost freezes.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mqttAndroidClient = new MqttAndroidClient(getActivity().getApplicationContext(), "tcp://xx.xx.xx.xx:1883", MqttClient.generateClientId());
        mqttAndroidClient.setCallback(new MqttCallbackExtended() {
            @Override
            public void connectComplete(boolean b, String s) {
            }
            @Override
            public void connectionLost(Throwable throwable) {
            }
            @Override
            public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
                Log.d("Mqtt", "messageArrived " + topic);
            }
            @Override
            public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();

        MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
        mqttConnectOptions.setAutomaticReconnect(true);
        mqttConnectOptions.setCleanSession(true);

        try {
            mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener() {
                @Override
                public void onSuccess(IMqttToken asyncActionToken) {
                    try {
                        mqttAndroidClient.subscribe("messages/#", 0);
                    } catch (Exception exception) {
                        Log.e("Mqtt", "subscribe error", exception);
                    }
                }
                @Override
                public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                    Log.e("Mqtt", "Connection onFailure ", exception);
                }
            });
        } catch (Exception exception) {
            Log.e("Mqtt", "error " + exception);
        }
    }       
1
Show us your actual code, especially the content of messageArrived callbackhardillb
@hardillb I updated the original question to include my actual implementation.devha
Usually there are no slashes in front of MQTT topic names...Alexander Farber
Thanks @AlexanderFarber I removed the leading slash (also updated the original question) but it seems that there is no difference in my android client performance.devha
I think the MqttAndroidClient communicates with the background service (which contains the actual client) using Intents(!) which are very CPU-intensive due to rights-management issues. What we would like to do is to not go via the extra service but implement our own... I'm thinking this might help, but I am not sure: github.com/eladnava/paho-mqtt-androidJohnyTex

1 Answers

0
votes

The problem is that Paho Android Service is not using own thread to handle connection so everything is done in UI thread.

I ended up using Paho MqttAsyncClient, which handles connection in own tread its own.