title: Can not connect to AWS Amplify PubSub -> Socket error:undefined
body: <p>I've been trying out all ways to get the <strong>Amplify/PubSub</strong> working without any luck. It seems all the documentation are rather outdated.</p>
  
  <p>Here is what I have done so far. Please note all hashes are made up ;-)</p>
  
  <ol>
  <li>Created a fresh React Native app</li>
  <li>Installed amplify packages</li>
  <li>Installed Amplify CLI</li>
  <li>Invoked <code>$ amplify configure</code></li>
  <li>Invoked <code>$ amplify init</code></li>
  <li>Invoked <code>$ amplify add auth</code></li>
  <li>Invoked <code>$ amplify push</code>, which created the <code>aws-exports.js</code> object</li>
  <li>Created a super simple component</li>
  </ol>
  
  <pre><code>    import React from 'react';
      import { View } from 'react-native';
  
      import { withAuthenticator } from 'aws-amplify-react-native';
      import Amplify, { Auth, PubSub } from 'aws-amplify';
      import { AWSIoTProvider } from '@aws-amplify/pubsub/lib/Providers';
  
      import awsmobile from './aws-exports';
  
      Amplify.configure({
        Auth: {
          mandatorySignIn: true,
          region: awsmobile.aws_cognito_region,
          userPoolId: awsmobile.aws_user_pools_id,
          identityPoolId: awsmobile.aws_cognito_identity_pool_id,
          userPoolWebClientId: awsmobile.aws_user_pools_web_client_id,
        },
        Analytics: {
          disabled: true,
        },
      });
  
      Amplify.addPluggable(
        new AWSIoTProvider({
          aws_pubsub_region: 'ap-southeast-2',
          aws_pubsub_endpoint:
        'wss://a123456789d-ats.iot.ap-southeast-2.amazonaws.com/mqtt',
        })
      );
  
      Amplify.Logger.LOG_LEVEL = 'DEBUG';
  
      class App extends PureComponent {
        componentDidMount() {
          if (this.props.authState === 'signedIn') {
            Auth.currentCredentials().then((creds) =&gt; {
  
              // get the principal that needs to be attached to policy
              console.log('principal to be attached', creds.identityId)
  
              PubSub.subscribe('topic_1').subscribe({
                next: (data) =&gt; console.log(JSON.stringify(data, null, 2)),
                error: (msg) =&gt; console.log('ERROR: ', msg.error),
                close: () =&gt; console.log('Done'),
              });
            });
          }
        }
  
        render() {
          return (
            &lt;View/&gt;
          );
        }
      }
  
      export default withAuthenticator(App);
  </code></pre>
  
  <ol start="9">
  <li>I attached AWS root certificate to my iPhone (see below)</li>
  </ol>
  
  <p><img src="https://i.stack.imgur.com/uJiqs.jpg" width="300" /></p>
  
  <p>10.Create a IAM policy for AWS IoT</p>
  
  <ul>
  <li><code>IoTAppPolicy</code></li>
  <li><code>iot:*</code></li>
  <li><code>arn:aws:iot:ap-southeast-2:1234567890:*</code></li>
  </ul>
  
  <p>11.Attach the principal I got from <code>Auth.currentCredentials</code> to the policy</p>
  
  <pre><code>aws iot attach-principal-policy --policy-name IoTAppPolicy --principal ap-southeast-2:db1234bc-5678-90123-4567-89ae0e123b4
  </code></pre>
  
  <p>12.Attach Policies to <code>Auth Role</code></p>
  
  <ul>
  <li><code>AWSIoTDataAccess</code></li>
  <li><code>AWSIoTConfigAccess</code></li>
  </ul>
  
  <p>Yet, when I run the app I get the following error log</p>
  
  <pre><code>[DEBUG] 51:42.745 SignIn - Sign In for test@test.com
  [DEBUG] 51:44.616 AuthClass CognitoUserSession {idToken: CognitoIdToken, refreshToken: CognitoRefreshToken, accessToken: CognitoAccessToken, clockDrift: 0}
  [DEBUG] 51:44.616 Credentials - set credentials from session
  [DEBUG] 51:46.247 Credentials - Load credentials successfully 
  [DEBUG] 51:46.248 AuthClass - succeed to get cognito credentials 
  DEBUG] 51:47.150 Hub - Dispatching to auth with  {event: "signIn", data: CognitoUser, message: "A user 2d...de has been signed in"}
  [DEBUG] 51:47.151 SignIn CognitoUser {username: "2d...de", pool: CognitoUserPool, Session: null, client: Client, signInUserSession: CognitoUserSession, …}
  [DEBUG] 51:47.152 AuthClass - Getting the session from this user: CognitoUser {username: "2d...de", pool: CognitoUserPool, Session: null, client: Client, signInUserSession: CognitoUserSession, …}
  [DEBUG] 51:47.152 AuthClass - Succeed to get the user session CognitoUserSession {idToken: CognitoIdToken, refreshToken: CognitoRefreshToken, accessToken: CognitoAccessToken, clockDrift: 1}
  [DEBUG] 51:47.574 AuthPiece - verified user attributes {verified: {…}, unverified: {…}}
  [INFO] 51:47.575 Authenticator - Inside handleStateChange method current authState: signIn
  [DEBUG] 51:47.575 VerifyContact - no unverified contact
  [INFO] 51:47.578 Authenticator - authState has been updated to signedIn
  [DEBUG] 51:47.581 AuthClass - getting current credentials
  [DEBUG] 51:47.582 Credentials - getting credentials
  [DEBUG] 51:47.582 Credentials - picking up credentials
  [DEBUG] 51:47.582 Credentials - getting new cred promise
  [DEBUG] 51:47.582 Credentials - checking if credentials exists and not expired
  [DEBUG] 51:47.583 Credentials - credentials not changed and not expired, directly return
  [DEBUG] 51:47.583 AnalyticsClass - Analytics has been disabled
  [DEBUG] 51:47.584 PubSub - subscribe options undefined
  [DEBUG] 51:47.584 MqttOverWSProvider - Subscribing to topic(s) topic1
  [DEBUG] 51:47.584 Credentials - getting credentials
  [DEBUG] 51:47.584 Credentials - picking up credentials
  [DEBUG] 51:47.584 Credentials - getting new cred promise
  [DEBUG] 51:47.585 Credentials - checking if credentials exists and not expired
  [DEBUG] 51:47.585 Credentials - are these credentials expired?
  [DEBUG] 51:47.585 Credentials - credentials not changed and not expired, directly return
  [DEBUG] 51:47.586 Signer {region: "ap-southeast-2", service: "iotdevicegateway"}
  [DEBUG] 51:47.590 MqttOverWSProvider - Creating new MQTT client cca4e07f-a15a-46ce-904d-483a83162018
  [WARN] 52:50.152 MqttOverWSProvider - cca4e07f-a15a-46ce-904d-483a83162018 {
    "errorCode": 7,
    "errorMessage": "AMQJS0007E Socket error:undefined.",
    "uri": "wss://a123456789d.iot.ap-southeast-2.amazonaws.com/mqtt?X-Amz-Algorithm=AWS4-HMAC-SHA256&amp;X-Amz-Credential=ASIAU7VGXF6UOWZIUFOM%2F20200502%2Fap-southeast-2%2Fiotdevicegateway%2Faws4_request&amp;X-Amz-Date=20200502T055147Z&amp;X-Amz-SignedHeaders=host&amp;X-Amz-Signature=010d8..7dba&amp;X-Amz-Security-Token=IQ..lk%3D"
  }
  ERROR: Disconnected, error code: 7
  [DEBUG] 52:50.153 MqttOverWSProvider - Unsubscribing from topic(s) topic1
  </code></pre>
  
  <p>Any idea why I can't connect to the topic?</p>