I'm working on establishing a websocket connection using aws-iot-device-sdk-js from my web application.
I have a rest api which will provide me the following and I'm able to establish the connection and subscribe to the topic. 1. accessKeyId 2. secretKey 3. sessionToken 4. expiration
The problem arises during re-connection of the web socket. It tries to reconnect using the same credentials which is used before.
I don't follow the cognito identity pool id approach to get the credentials(used in the below example) and instead use the REST API mentioned above.
https://github.com/aws/aws-iot-device-sdk-js/blob/master/examples/browser/mqtt-webpack/entry.js
I'm able to execute a function based on expiration time and disconnect and connect again with the new credentials.
let watchCredentialsExpiration = (device, expiration) => {
let diff = moment(expiration).diff(moment());
let ms = moment.duration(diff).asMilliseconds();
ms < 1
? reEstablishConnection(device)
: $window.setTimeout(reEstablishConnection.bind(this, device), ms);
}
But I want to know if there is any better approach.
Aucun commentaire:
Enregistrer un commentaire