Aws-sdk-android: MqttException (0) - java.io.IOException: Already connected

Created on 9 Apr 2020  路  4Comments  路  Source: aws-amplify/aws-sdk-android

Describe the bug
I'am using android sdk. When I want to make a custom authorization with mqtt it says you are "Already connected". And I can see the connection in the logs but after 1 second it kicks me out. And there is no proper sample app for custom authorization.

To Reproduce
A code sample or steps:
I am using the Amplify pubsub tutorials code

Which AWS service(s) are affected?

Expected behavior
In iOS there is no connection error in same method but when we try in the Android platform it says already connected.

Screenshots
https://imgur.com/G29NjHR

Environment Information (please complete the following information):

  • AWS Android SDK Version: [e.g. 2.6.25]
  • Device: Samsung S10E , Nexus 5
  • Android Version: Android 10 , Android 7.1
  • Specific to simulators: No
Bug IoT

Most helpful comment

It looks like Paho returns "Already connected" on a connection error due to authorization under WebSockets (see https://github.com/eclipse/paho.mqtt.java/issues/481). If you don't have a Policy Document that allows your connection to subscribe to topics, that's the result you get. I was able to get this working with the following (much-too-wide, not-following-the-principle-of-least-privilege) Policy Document:

$ aws iot test-invoke-authorizer --authorizer-name [AuthorizerName] --token [TokenName] --token-signature [Signature]
{
    "isAuthenticated": true,
    "principalId": "user",
    "policyDocuments": [
        "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Action\":\"iot:*\",\"Effect\":\"Allow\",\"Resource\":\"arn:aws:iot:us-east-1:012345678912:*\"}]}"
    ],
    "refreshAfterInSeconds": 300,
    "disconnectAfterInSeconds": 86400
}

Let me know if this resolves your issue. I'll provide some feedback on the IoT docs around this, and we'll work with the Paho project to see if we can help resolve that bug.

All 4 comments

Thanks, @cmbaykal. Can you share the same code you've written so we can look at this?

try {
String clientId = MqttAsyncClient.generateClientId();

        AWSIotMqttManager manager = new AWSIotMqttManager(clientId, Region.getRegion(Regions.US_EAST_1), "a3p843671svo75-ats");

        manager.setCleanSession(true);
        manager.setAutoReconnect(true);
        manager.setKeepAlive(60);

        AccessTokenHelper helper = new AccessTokenHelper(TokenHelper.accessToken);

        String tokenKeyName = "token-name";
        String token = "token-access";
        String tokenSignature = "signature";
        String customAuthorizerName = "token-authorizer";

        manager.connect(
                tokenKeyName,
                token,
                tokenSignature,
                customAuthorizerName,
                (status, throwable) -> {
                    Log.d("AWSTest", "Status = " + status);
                    activity.runOnUiThread(() -> {
                        if (throwable != null) {
                            Log.e("AWSTest", "Connection error.", throwable);
                        }
                    });
                });
    } catch (final Exception e) {
        Log.e("AWSTest", "Connection error.", e);
    }

Using the example code you provided, I'm able to reproduce this.

It looks like Paho returns "Already connected" on a connection error due to authorization under WebSockets (see https://github.com/eclipse/paho.mqtt.java/issues/481). If you don't have a Policy Document that allows your connection to subscribe to topics, that's the result you get. I was able to get this working with the following (much-too-wide, not-following-the-principle-of-least-privilege) Policy Document:

$ aws iot test-invoke-authorizer --authorizer-name [AuthorizerName] --token [TokenName] --token-signature [Signature]
{
    "isAuthenticated": true,
    "principalId": "user",
    "policyDocuments": [
        "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Action\":\"iot:*\",\"Effect\":\"Allow\",\"Resource\":\"arn:aws:iot:us-east-1:012345678912:*\"}]}"
    ],
    "refreshAfterInSeconds": 300,
    "disconnectAfterInSeconds": 86400
}

Let me know if this resolves your issue. I'll provide some feedback on the IoT docs around this, and we'll work with the Paho project to see if we can help resolve that bug.

Was this page helpful?
0 / 5 - 0 ratings