diff --git a/iot/http_example/README.md b/iot/http_example/README.md index a4e2ed9b55..923fd61140 100644 --- a/iot/http_example/README.md +++ b/iot/http_example/README.md @@ -34,6 +34,7 @@ Options: Either RS256 (RSA) or ES256 (Eliptic Curve) --cloud_region [region] GCP cloud region --num_messages [num] Number of messages to publish. + --token_exp_mins [num] Minutes to JWT token expiration. --http_bridge_address [address] HTTP bridge address. --message_type [events|state] The message type to publish. diff --git a/iot/http_example/cloudiot_http_example_nodejs.js b/iot/http_example/cloudiot_http_example_nodejs.js index 3b98e5ec83..50a88b4288 100644 --- a/iot/http_example/cloudiot_http_example_nodejs.js +++ b/iot/http_example/cloudiot_http_example_nodejs.js @@ -66,6 +66,12 @@ var argv = require(`yargs`) requiresArg: true, type: 'number' }, + token_exp_mins: { + default: 20, + description: 'Minutes to JWT token expiration.', + requiresArg: true, + type: 'number' + }, http_bridge_address: { default: 'cloudiot-device.googleapis.com', description: 'HTTP bridge address.', @@ -145,6 +151,14 @@ function publishAsync (messageCount, numMessages) { // If we have published fewer than numMessage messages, publish payload // messageCount + 1. setTimeout(function () { + let secsFromIssue = parseInt(Date.now() / 1000) - iatTime; + if (secsFromIssue > argv.token_exp_mins * 60) { + iatTime = parseInt(Date.now() / 1000); + console.log(`\tRefreshing token after ${secsFromIssue} seconds.`); + + authToken = createJwt(argv.project_id, argv.private_key_file, argv.algorithm); + } + publishAsync(messageCount + 1, numMessages); }, delayMs); } @@ -161,7 +175,8 @@ const devicePath = `projects/${argv.project_id}/locations/${argv.cloud_region}/r const pathSuffix = argv.message_type === 'events' ? ':publishEvent' : ':setState'; const url = `https://${argv.http_bridge_address}/v1beta1/${devicePath}${pathSuffix}`; -const authToken = createJwt(argv.project_id, argv.private_key_file, argv.algorithm); +let iatTime = parseInt(Date.now() / 1000); +let authToken = createJwt(argv.project_id, argv.private_key_file, argv.algorithm); // Publish messages. publishAsync(1, argv.num_messages);