Skip to content

Commit 275bcdb

Browse files
committed
Add the updateToken method to reconnect after a token refresh
1 parent 87a16f9 commit 275bcdb

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ ArduinoCloud.onPropertyValue(thingId, propertyName, updateCb).then(() => {
6969
// updateCb(message) will be called every time a new value is available. Value can be string, number, or a boolean depending on the property type
7070
});
7171

72+
// Re-connect with a new authentication token, keeping the subscriptions
73+
// to the Things topics
74+
ArduinoCloud.updateToken(newToken).then(() => {
75+
// Successful reconnection with the provided new token
76+
});
77+
7278
```
7379

7480
## Run tests

src/index.js

+38
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import CBOR from 'cbor-js';
4949
import ArduinoCloudError from './ArduinoCloudError';
5050

5151
let connection = null;
52+
let connectionOptions = null;
5253
const subscribedTopics = {};
5354
const propertyCallback = {};
5455
const arduinoCloudPort = 8443;
@@ -82,6 +83,8 @@ const connect = options => new Promise((resolve, reject) => {
8283
onConnected: options.onConnected,
8384
};
8485

86+
connectionOptions = opts;
87+
8588
if (connection) {
8689
return reject(new Error('connection failed: connection already open'));
8790
}
@@ -222,6 +225,40 @@ const disconnect = () => new Promise((resolve, reject) => {
222225
return resolve();
223226
});
224227

228+
const updateToken = token => new Promise((resolve, reject) => {
229+
if (!connection) {
230+
return reject(new Error('disconnection failed: connection closed'));
231+
}
232+
233+
try {
234+
// Disconnect to the connection using the old token
235+
connection.disconnect();
236+
} catch (error) {
237+
// Ignore disconnection errors that comes out when Paho is reconnecting
238+
}
239+
240+
// Remove the connection
241+
connection = null;
242+
243+
return resolve();
244+
})
245+
.then(() => {
246+
// Reconnect using the new token
247+
const reconnectOptions = Object.assign({}, connectionOptions, { token });
248+
return connect(reconnectOptions);
249+
})
250+
.then(() => {
251+
// Re-subscribe to all topics subscribed before the reconnection
252+
Object.values(subscribedTopics).forEach((subscribeParams) => {
253+
subscribe(subscribeParams.topic, subscribeParams.cb);
254+
});
255+
256+
if (typeof connectionOptions.onConnected === 'function') {
257+
// Call the connection callback (with the reconnection param set to true)
258+
connectionOptions.onConnected(true);
259+
}
260+
});
261+
225262
const subscribe = (topic, cb) => new Promise((resolve, reject) => {
226263
if (!connection) {
227264
return reject(new Error('subscription failed: connection closed'));
@@ -399,6 +436,7 @@ const onPropertyValue = (thingId, name, cb) => {
399436
export default {
400437
connect,
401438
disconnect,
439+
updateToken,
402440
subscribe,
403441
unsubscribe,
404442
sendMessage,

0 commit comments

Comments
 (0)