AWS IOT reconnect issue

Viewed 151

I am using AWS IOT for the socket connection. Below is the code which connects the client to the AWS IoT. Everything works fine until the browser tab becomes idle for a while.

initMqttClient = ->
  mqttClient = AWSIoTData.device(
    region: AWS.config.region
    host: "<%= ENV['MQTT_HOST'] %>"
    clientId: 'mqtt-client-' + (Math.floor((Math.random() * 100000) + 1))
    protocol: 'wss'
    maximumReconnectTimeMs: 8000
    offlineQueueing: true
    debug: true
    accessKeyId: ''
    secretKey: ''
    sessionToken: '')


authenticateClient = ->
  cognitoIdentity = new (AWS.CognitoIdentity)
  AWS.config.credentials.get (err, data) ->
    if !err
      params = IdentityId: AWS.config.credentials.identityId
      cognitoIdentity.getCredentialsForIdentity params, (err, data) ->
        if !err
          # Update our latest AWS credentials; the MQTT client will use these
          # during its next reconnect attempt.
          mqttClient.updateWebSocketCredentials(data.Credentials.AccessKeyId,
              data.Credentials.SecretKey,
              data.Credentials.SessionToken);
        else
          console.log 'error retrieving credentials: ' + err
        return
    else
      console.log 'error retrieving identity:' + err
    return

The socket reconnects fails when trying to reconnect. Attached screenshot of the same enter image description here.

Is there anything that I am missing here?

1 Answers

you must recreated the signed url.

Related