Skip to content

Commit 65b9bfa

Browse files
committed
remaining mqtt5 samples
1 parent 3354c07 commit 65b9bfa

16 files changed

Lines changed: 400 additions & 76 deletions

File tree

samples/node/mqtt/mqtt5_aws_websocket/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ To Run this sample from the `samples/node/mqtt/aws_websocket` folder, use the fo
7171

7272
```sh
7373
npm install
74-
node index.js \
74+
node dist/index.js \
7575
--endpoint <AWS IoT endpoint> \
7676
--signing_region <AWS region>
7777
```
7878
If you would like to see what optional arguments are available, use the `--help` argument:
7979
``` sh
80-
node index.js --help
80+
node dist/index.js --help
8181
```
8282

8383
will result in the following output:

samples/node/mqtt/mqtt5_aws_websocket/index.js renamed to samples/node/mqtt/mqtt5_aws_websocket/index.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
* SPDX-License-Identifier: Apache-2.0.
44
*/
55

6-
const { mqtt5, iot } = require("aws-iot-device-sdk-v2");
7-
const { once } = require("events");
8-
const yargs = require('yargs');
9-
const { v4: uuidv4 } = require('uuid');
6+
import { mqtt5, iot } from "aws-iot-device-sdk-v2";
7+
import { once } from "events";
8+
import yargs from "yargs";
9+
import { v4 as uuidv4 } from "uuid";
1010

1111
const TIMEOUT = 100000;
1212

@@ -74,7 +74,7 @@ async function runSample() {
7474
const client = new mqtt5.Mqtt5Client(config);
7575

7676
// Event handler for when any message is received
77-
client.on('messageReceived', (eventData) => {
77+
client.on('messageReceived', (eventData: mqtt5.MessageReceivedEvent) => {
7878
const message = eventData.message;
7979
const payload = message.payload ? Buffer.from(message.payload).toString('utf-8') : '';
8080
console.log(`==== Received message from topic '${message.topicName}': ${payload} ====\n`);
@@ -96,17 +96,17 @@ async function runSample() {
9696
});
9797

9898
// Event handler for lifecycle event Connection Success
99-
client.on('connectionSuccess', (eventData) => {
99+
client.on('connectionSuccess', (eventData: mqtt5.ConnectionSuccessEvent) => {
100100
console.log(`Lifecycle Connection Success with reason code: ${eventData.connack.reasonCode}\n`);
101101
});
102102

103103
// Event handler for lifecycle event Connection Failure
104-
client.on('connectionFailure', (eventData) => {
104+
client.on('connectionFailure', (eventData: mqtt5.ConnectionFailureEvent) => {
105105
console.log(`Lifecycle Connection Failure with exception: ${eventData.error}`);
106106
});
107107

108108
// Event handler for lifecycle event Disconnection
109-
client.on('disconnection', (eventData) => {
109+
client.on('disconnection', (eventData: mqtt5.DisconnectionEvent) => {
110110
const reasonCode = eventData.disconnect ? eventData.disconnect.reasonCode : 'None';
111111
console.log(`Lifecycle Disconnected with reason code: ${reasonCode}`);
112112
});
@@ -120,7 +120,7 @@ async function runSample() {
120120
const connectionSuccess = once(client, "connectionSuccess");
121121
await Promise.race([
122122
connectionSuccess,
123-
new Promise((_, reject) => setTimeout(() => reject(new Error("Connection timeout")), TIMEOUT))
123+
new Promise<never>((_, reject) => setTimeout(() => reject(new Error("Connection timeout")), TIMEOUT))
124124
]);
125125

126126
console.log(`==== Subscribing to topic '${args.topic}' ====`);
@@ -149,17 +149,17 @@ async function runSample() {
149149
payload: message,
150150
qos: mqtt5.QoS.AtLeastOnce
151151
});
152-
console.log(`PubAck received with ${publishResult.reasonCode}\n`);
152+
console.log(`PubAck received with ${publishResult?.reasonCode}\n`);
153153

154-
await new Promise(resolve => setTimeout(resolve, 1500));
154+
await new Promise<void>(resolve => setTimeout(resolve, 1500));
155155
publishCount++;
156156
}
157157

158158
if (receivedCount < args.count) {
159159
const receivedAll = once(client, "receivedAll");
160160
await Promise.race([
161161
receivedAll,
162-
new Promise(resolve => setTimeout(resolve, 5000))
162+
new Promise<void>(resolve => setTimeout(resolve, 5000))
163163
]);
164164
}
165165
console.log(`${receivedCount} message(s) received.\n`);
@@ -177,7 +177,7 @@ async function runSample() {
177177

178178
await Promise.race([
179179
stopped,
180-
new Promise((_, reject) => setTimeout(() => reject(new Error("Stop timeout")), TIMEOUT))
180+
new Promise<never>((_, reject) => setTimeout(() => reject(new Error("Stop timeout")), TIMEOUT))
181181
]);
182182

183183
console.log("==== Client Stopped! ====");
@@ -186,7 +186,7 @@ async function runSample() {
186186

187187
runSample().then(() => {
188188
process.exit(0);
189-
}).catch((error) => {
189+
}).catch((error: Error) => {
190190
console.error(error);
191191
process.exit(1);
192192
});

samples/node/mqtt/mqtt5_aws_websocket/package.json

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,27 @@
22
"name": "mqtt5-aws-websocket-sample",
33
"version": "1.0.0",
44
"description": "MQTT5 AWS Websocket Sample",
5-
"main": "index.js",
5+
"homepage": "https://github.com/aws/aws-iot-device-sdk-js-v2",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/aws/aws-iot-device-sdk-js-v2.git"
9+
},
10+
"contributors": [
11+
"AWS SDK Common Runtime Team <aws-sdk-common-runtime@amazon.com>"
12+
],
13+
"license": "Apache-2.0",
14+
"main": "dist/index.js",
615
"scripts": {
7-
"start": "node index.js"
16+
"tsc": "tsc",
17+
"prepare": "npm run tsc"
818
},
919
"dependencies": {
1020
"aws-iot-device-sdk-v2": "file:../../../..",
11-
"yargs": "^17.0.0",
21+
"yargs": "^16.2.0",
1222
"uuid": "^9.0.0"
23+
},
24+
"devDependencies": {
25+
"@types/node": "10.17.50",
26+
"typescript": "^4.7.4"
1327
}
1428
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"compilerOptions": {
3+
/* Basic Options */
4+
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
5+
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
6+
"declaration": true, /* Generates corresponding '.d.ts' file. */
7+
"sourceMap": true, /* Generates corresponding '.map' file. */
8+
"outDir": "./dist", /* Redirect output structure to the directory. */
9+
// "lib": [], /* Specify library files to be included in the compilation. */
10+
// "allowJs": true, /* Allow javascript files to be compiled. */
11+
// "checkJs": true, /* Report errors in .js files. */
12+
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
13+
// "outFile": "./", /* Concatenate and emit output to single file. */
14+
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
15+
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
16+
// "composite": true, /* Enable project compilation */
17+
// "removeComments": false, /* Do not emit comments to output. */
18+
// "noEmit": true, /* Do not emit outputs. */
19+
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
20+
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
21+
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
22+
23+
/* Strict Type-Checking Options */
24+
"strict": true, /* Enable all strict type-checking options. */
25+
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
26+
"strictNullChecks": true, /* Enable strict null checks. */
27+
"strictFunctionTypes": true, /* Enable strict checking of function types. */
28+
"strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
29+
"strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
30+
"noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
31+
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
32+
33+
/* Additional Checks */
34+
"noUnusedLocals": true, /* Report errors on unused locals. */
35+
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
36+
// "noUnusedParameters": true, /* Report errors on unused parameters. */
37+
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
38+
39+
/* Module Resolution Options */
40+
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
41+
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
42+
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
43+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
44+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
45+
// "typeRoots": [], /* List of folders to include type definitions from. */
46+
// "types": [], /* Type declaration files to be included in compilation. */
47+
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
48+
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
49+
50+
/* Source Map Options */
51+
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
52+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
53+
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
54+
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
55+
56+
/* Experimental Options */
57+
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
58+
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
59+
},
60+
"include": [
61+
"*.ts"
62+
],
63+
"exclude": [
64+
"node_modules",
65+
"dist"
66+
]
67+
}

samples/node/mqtt/mqtt5_custom_auth_signed/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ To Run this sample from the `samples/node/mqtt/mqtt5_custom_auth_signed` folder,
7070

7171
```sh
7272
npm install
73-
node index.js \
73+
node dist/index.js \
7474
--endpoint <AWS IoT endpoint> \
7575
--authorizer_name <custom authorizer name> \
7676
--auth_signature <signature> \
@@ -79,7 +79,7 @@ node index.js \
7979
```
8080
If you would like to see what optional arguments are available, use the `--help` argument:
8181
``` sh
82-
node index.js --help
82+
node dist/index.js --help
8383
```
8484

8585
will result in the following output:

samples/node/mqtt/mqtt5_custom_auth_signed/index.js renamed to samples/node/mqtt/mqtt5_custom_auth_signed/index.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
* SPDX-License-Identifier: Apache-2.0.
44
*/
55

6-
const { mqtt5, iot } = require("aws-iot-device-sdk-v2");
7-
const { once } = require("events");
8-
const yargs = require('yargs');
9-
const { v4: uuidv4 } = require('uuid');
6+
import { mqtt5, iot } from "aws-iot-device-sdk-v2";
7+
import { once } from "events";
8+
import yargs from "yargs";
9+
import { v4 as uuidv4 } from "uuid";
1010

1111
const TIMEOUT = 100000;
1212

@@ -118,7 +118,7 @@ async function runSample() {
118118
const client = new mqtt5.Mqtt5Client(config);
119119

120120
// Event handler for when any message is received
121-
client.on('messageReceived', (eventData) => {
121+
client.on('messageReceived', (eventData: mqtt5.MessageReceivedEvent) => {
122122
const message = eventData.message;
123123
const payload = message.payload ? Buffer.from(message.payload).toString('utf-8') : '';
124124
console.log(`==== Received message from topic '${message.topicName}': ${payload} ====\n`);
@@ -140,17 +140,17 @@ async function runSample() {
140140
});
141141

142142
// Event handler for lifecycle event Connection Success
143-
client.on('connectionSuccess', (eventData) => {
143+
client.on('connectionSuccess', (eventData: mqtt5.ConnectionSuccessEvent) => {
144144
console.log(`Lifecycle Connection Success with reason code: ${eventData.connack.reasonCode}\n`);
145145
});
146146

147147
// Event handler for lifecycle event Connection Failure
148-
client.on('connectionFailure', (eventData) => {
148+
client.on('connectionFailure', (eventData: mqtt5.ConnectionFailureEvent) => {
149149
console.log(`Lifecycle Connection Failure with exception: ${eventData.error}`);
150150
});
151151

152152
// Event handler for lifecycle event Disconnection
153-
client.on('disconnection', (eventData) => {
153+
client.on('disconnection', (eventData: mqtt5.DisconnectionEvent) => {
154154
const reasonCode = eventData.disconnect ? eventData.disconnect.reasonCode : 'None';
155155
console.log(`Lifecycle Disconnected with reason code: ${reasonCode}`);
156156
});
@@ -164,7 +164,7 @@ async function runSample() {
164164
const connectionSuccess = once(client, "connectionSuccess");
165165
await Promise.race([
166166
connectionSuccess,
167-
new Promise((_, reject) => setTimeout(() => reject(new Error("Connection timeout")), TIMEOUT))
167+
new Promise<never>((_, reject) => setTimeout(() => reject(new Error("Connection timeout")), TIMEOUT))
168168
]);
169169

170170
console.log(`==== Subscribing to topic '${args.topic}' ====`);
@@ -193,17 +193,17 @@ async function runSample() {
193193
payload: message,
194194
qos: mqtt5.QoS.AtLeastOnce
195195
});
196-
console.log(`PubAck received with ${publishResult.reasonCode}\n`);
196+
console.log(`PubAck received with ${publishResult?.reasonCode}\n`);
197197

198-
await new Promise(resolve => setTimeout(resolve, 1500));
198+
await new Promise<void>(resolve => setTimeout(resolve, 1500));
199199
publishCount++;
200200
}
201201

202202
if (receivedCount < args.count) {
203203
const receivedAll = once(client, "receivedAll");
204204
await Promise.race([
205205
receivedAll,
206-
new Promise(resolve => setTimeout(resolve, 5000))
206+
new Promise<void>(resolve => setTimeout(resolve, 5000))
207207
]);
208208
}
209209
console.log(`${receivedCount} message(s) received.\n`);
@@ -221,7 +221,7 @@ async function runSample() {
221221

222222
await Promise.race([
223223
stopped,
224-
new Promise((_, reject) => setTimeout(() => reject(new Error("Stop timeout")), TIMEOUT))
224+
new Promise<never>((_, reject) => setTimeout(() => reject(new Error("Stop timeout")), TIMEOUT))
225225
]);
226226

227227
console.log("==== Client Stopped! ====");
@@ -230,7 +230,7 @@ async function runSample() {
230230

231231
runSample().then(() => {
232232
process.exit(0);
233-
}).catch((error) => {
233+
}).catch((error: Error) => {
234234
console.error(error);
235235
process.exit(1);
236236
});

samples/node/mqtt/mqtt5_custom_auth_signed/package.json

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,27 @@
22
"name": "mqtt5-custom-auth-signed-sample",
33
"version": "1.0.0",
44
"description": "MQTT5 Signed Custom Authorizer Sample",
5-
"main": "index.js",
5+
"homepage": "https://github.com/aws/aws-iot-device-sdk-js-v2",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/aws/aws-iot-device-sdk-js-v2.git"
9+
},
10+
"contributors": [
11+
"AWS SDK Common Runtime Team <aws-sdk-common-runtime@amazon.com>"
12+
],
13+
"license": "Apache-2.0",
14+
"main": "dist/index.js",
615
"scripts": {
7-
"start": "node index.js"
16+
"tsc": "tsc",
17+
"prepare": "npm run tsc"
818
},
919
"dependencies": {
1020
"aws-iot-device-sdk-v2": "file:../../../..",
11-
"yargs": "^17.0.0",
21+
"yargs": "^16.2.0",
1222
"uuid": "^9.0.0"
23+
},
24+
"devDependencies": {
25+
"@types/node": "10.17.50",
26+
"typescript": "^4.7.4"
1327
}
1428
}

0 commit comments

Comments
 (0)