Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[paho-dev] Troubleshooting persistent connection issues with Paho and Android

Hey guys,

I communicated with some of you about a month ago regarding issues I was having using the Paho API in an Android app I was working on; since then, I've managed to get messages to deliver and everything is working a lot better.  I switched to using an EC2 instance as my broker instead of my own server (still using Mosquitto though) and that seemed to fix it...I'm not sure why.  Either way, the main issue I'm having now is that messages only deliver from the broker to the client after the client sends something to the broker.  Do I need to continually ping the server while the app is running in the foreground?  I know the Facebook Messenger app uses MQTT, so I thought that the client should be able to receive messages on demand of the broker, not only at a time when the client seeks them out.

In addition, I'm getting LOADS of duplicate messages.  All my messages are sent with a QoS of 2, so I thought that shouldn't happen...Either way, I put in some checks against duplicate messages getting added to my app's data model, so they aren't really causing a problem besides wasted data and runtime.  Still, if it's a problem I can fix I would really like to.

Below I'll include some of the Paho-relevant code from the app.  On my mosquitto.conf file, I'm using user/password authentication and allowing for 10 in-flight messages at a time (I intended to included more from the conf file in here, but I'm having trouble ftp-ing into my server from work...I can attach it later if it would help).  Sorry for the extreme length!  If you have any advice as to where this problem might be coming from, or if there's anything I could debug through that would give more information, please let me know!  Thank you!!

Connection set up:

public void setUpService(final Context p_context){
........
        try {
            _mqttClient = new MqttAsyncClient(_mqttServer, _clientId, new MqttDefaultFilePersistence(p_context.getFilesDir().getAbsolutePath()));
            _mqttClient.setCallback(this);
            _mqttOptions.setUserName(_mqttUserName);
            _mqttOptions.setPassword(_mqttPassword.toCharArray());
            _mqttOptions.setCleanSession(false);
            _mqttOptions.setKeepAliveInterval(_keepAlive);
            _mqttOptions.setConnectionTimeout(_connectionTimeout);
            IMqttToken token = _mqttClient.connect(_mqttOptions);
            token.waitForCompletion();
            IMqttToken token2 = _mqttClient.subscribe("us", 2);
            token2.waitForCompletion();
        } catch (MqttException e) {
            handleMqttException(e);
        }
        ......
    }

Publishing override:

private void publishMessage(TransmissionPayload p_payload){
        if (!_mqttClient.isConnected()){
            try {
                IMqttToken token = _mqttClient.connect(_mqttOptions);
                token.waitForCompletion();
            } catch (MqttException e) { handleMqttException(e); }
        }

        JSONObject tx = new JSONObject(p_payload.getMap());
        ((NotificationManager)getSystemService(NOTIFICATION_SERVICE)).cancelAll();

        try {
            _mqttClient.publish(_topic, tx.toString().getBytes(_encoding), 2, false);
        } catch (MqttException e) { handleMqttException(e); } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

***For clarification, _topic = "us", _keepAlive = 36000, _connectionTimeout = 30.

Thanks again for reading, and in advance for any of your help!

Regards,
Tucker

--

Back to the top