aws / aws-iot-device-sdk-js

SDK for connecting to AWS IoT from a device using JavaScript/Node.js
Apache License 2.0
965 stars 385 forks source link

AWS IOT reconnect issue after the browser tab goes idle for a while #400

Closed aarthi-mallow closed 2 years ago

aarthi-mallow commented 2 years ago

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. Is there anything that I am missing here?

Screen Shot 2022-01-28 at 9 02 24 PM Screen Shot 2022-01-29 at 9 40 54 AM

I came across this issue(https://github.com/aws/aws-iot-device-sdk-js/issues/393) and we are following everything suggested there(Using ats endpoint, using different clientId for each tab, we are not clearing local storage).

jmklix commented 2 years ago

Which browser is this happening in? Does this happen in other browsers as well?

bsrikanth88 commented 2 years ago

@aarthi-mallow did you find any solution for reconnecting. I am also facing the same issue. If the network is back within 10 min it is successfully able to reconnect, after that, it tries to establish the connection even there was a network.

github-actions[bot] commented 2 years ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.