Skip to content

Commit b697320

Browse files
authored
React sample (#378)
* react sample * improve error handling and readme * update readme * EOF * update sample readme * update cr comments
1 parent 9f20a4c commit b697320

File tree

11 files changed

+699
-3
lines changed

11 files changed

+699
-3
lines changed

documents/FAQ.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Please note that on Mac, once a private key is used with a certificate, that cer
4141
static: certificate has an existing certificate-key pair that was previously imported into the Keychain. Using key from Keychain instead of the one provided.
4242
```
4343

44-
### How do debug in VSCode?
44+
### How do debug in VSCode?
4545

4646
Here is an example launch.json file to run the pubsub sample
4747
``` json
@@ -83,13 +83,28 @@ Here is an example launch.json file to run the pubsub sample
8383
* Device certificate
8484
* Intermediate device certificate that is used to generate the key below
8585
* When using samples it can look like this: `--cert abcde12345-certificate.pem.crt`
86-
* Key files
86+
* Key files
8787
* You should have generated/downloaded private and public keys that will be used to verify that communications are coming from you
8888
* When using samples you only need the private key and it will look like this: `--key abcde12345-private.pem.key`
8989

90+
91+
### I would like to build a browser application and got error "Property does not exist on type 'typeof import("\<path\>/node_modules/aws-crt/dist/**native**/*")
92+
93+
The aws-iot-device-sdk-v2 library consists of two parts, [node(native)](https://aws.github.io/aws-iot-device-sdk-js-v2/node/index.html) and [browser](https://aws.github.io/aws-iot-device-sdk-js-v2/browser/index.html). The library will access the **native** API by default. You can configure the path in the `tsconfig.json` file to ensure that the app utilizes the **browser** API.
94+
95+
To set up the path in tsconfig.json, you can add a mapping for the library module that specifies the desired path for the browser API. Here's an example:
96+
97+
`tsconfig.json`:
98+
```
99+
"paths": {
100+
"aws-iot-device-sdk-v2": ["node_modules/aws-iot-device-sdk-v2/dist/browser"]
101+
},
102+
```
103+
104+
90105
### I still have more questions about the this sdk?
91106

92107
* [Here](https://docs.aws.amazon.com/iot/latest/developerguide/what-is-aws-iot.html) are the AWS IoT Core docs for more details about IoT Core
93-
* [Here](https://docs.aws.amazon.com/greengrass/v2/developerguide/what-is-iot-greengrass.html) are the AWS IoT Greengrass v2 docs for more details about greengrass
108+
* [Here](https://docs.aws.amazon.com/greengrass/v2/developerguide/what-is-iot-greengrass.html) are the AWS IoT Greengrass v2 docs for more details about greengrass
94109
* [Discussion](https://github.com/aws/aws-iot-device-sdk-js-v2/discussions) questions are also a great way to ask other questions about this sdk.
95110
* [Open an issue](https://github.com/aws/aws-iot-device-sdk-js-v2/issues) if you find a bug or have a feature request

samples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* [Node: MQTT5 Pub/Sub](./node/pub_sub_mqtt5/README.md)
44
* [Browser: MQTT5 Pub/Sub](./browser/pub_sub_mqtt5/README.md)
5+
* [Browser: MQTT5 Pub/Sub with React](./browser/react_sample/README.md)
56
* [Node: Pub/Sub](./node/pub_sub/README.md)
67
* [Pub/Sub JS](./node/pub_sub_js/README.md)
78
* [Browser: Pub/Sub](./browser/pub_sub/README.md)
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Browser: MQTT5 PubSub with React
2+
3+
[**Return to main sample list**](../../README.md)
4+
5+
This sample uses the
6+
[Message Broker](https://docs.aws.amazon.com/iot/latest/developerguide/iot-message-broker.html)
7+
for AWS IoT to send and receive messages through an MQTT connection using MQTT5/MQTT311 in the browser with React library.
8+
9+
MQTT5 introduces additional features and enhancements that improve the development experience with MQTT. You can read more about MQTT5 in the Java V2 SDK by checking out the [MQTT5 user guide](https://github.com/awslabs/aws-crt-nodejs/blob/main/MQTT5-UserGuide.md).
10+
11+
Note: MQTT5 support is currently in **developer preview**. We encourage feedback at all times, but feedback during the preview window is especially valuable in shaping the final product. During the preview period we may make backwards-incompatible changes to the public API, but in general, this is something we will try our best to avoid.
12+
13+
Your IoT Core Thing's [Policy](https://docs.aws.amazon.com/iot/latest/developerguide/iot-policies.html) must provide privileges for this sample to connect, subscribe, publish, and receive. Below is a sample policy that can be used on your IoT Core Thing that will allow this sample to run as intended.
14+
15+
<details>
16+
<summary>(see sample policy)</summary>
17+
<pre>
18+
{
19+
"Version": "2012-10-17",
20+
"Statement": [
21+
{
22+
"Effect": "Allow",
23+
"Action": [
24+
"iot:Publish",
25+
"iot:Receive"
26+
],
27+
"Resource": [
28+
"arn:aws:iot:<b>region</b>:<b>account</b>:topic/test/topic*"
29+
]
30+
},
31+
{
32+
"Effect": "Allow",
33+
"Action": [
34+
"iot:Subscribe"
35+
],
36+
"Resource": [
37+
"arn:aws:iot:<b>region</b>:<b>account</b>:topicfilter/test/topic*",
38+
]
39+
},
40+
{
41+
"Effect": "Allow",
42+
"Action": [
43+
"iot:Connect"
44+
],
45+
"Resource": [
46+
"arn:aws:iot:<b>region</b>:<b>account</b>:client/test-*"
47+
]
48+
}
49+
]
50+
}
51+
</pre>
52+
53+
Replace with the following with the data from your AWS account:
54+
* `<region>`: The AWS IoT Core region where you created your AWS IoT Core thing you wish to use with this sample. For example `us-east-1`.
55+
* `<account>`: Your AWS IoT Core account ID. This is the set of numbers in the top right next to your AWS account name when using the AWS IoT Core website.
56+
57+
Note that in a real application, you may want to avoid the use of wildcards in your ClientID or use them selectively. Please follow best practices when working with AWS on production applications using the SDK. Also, for the purposes of this sample, please make sure your policy allows a client ID of `test-*` to connect or use `--client_id <client ID here>` to send the client ID your policy supports.
58+
59+
</details>
60+
61+
## How to run
62+
63+
To run this sample you need to have a Cognito identity pool setup that can be used for making IoT connections. To see how to setup a Cognito identity pool, please refer to this page on the [AWS documentation](https://docs.aws.amazon.com/cognito/latest/developerguide/tutorial-create-identity-pool.html).
64+
65+
Once you have a Cognito identity pool, you need to fill in the credentials in the `browser/react_sample/src/settings.ts` file with your AWS endpoint, AWS region, and Cognito identity pool.
66+
67+
Run `npm install` in the `browser/react_sample` folder to build the sample.
68+
Then run `npm start` to start the dev server. The default port is `3000`. You should be able to run the sample at http://localhost:3000/ in your browser. If configured correctly, it should connect to AWS IoT Core, subscribe to a topic, and then start receiving the messages it publishes to the topic it's subscribed to.
69+
70+
You can adjust the port at [package.json](./package.json):
71+
```
72+
"scripts": {
73+
"test": "echo \"Error: no test specified\" && exit 1",
74+
"start": "webpack serve --port 3000",
75+
"build": "NODE_ENV=production webpack"
76+
},
77+
```
78+
79+
## Mqtt311 Sample
80+
By default, the sample uses Mqtt5. To checkout the sample with Mqtt311. You can modify the [index.tsx](./src/index.tsx)
81+
```
82+
root.render(
83+
// <Mqtt5 />
84+
// Comment the Mqtt5 and uncomment Mqtt311 to enable the Mqtt311 component
85+
<Mqtt311 />
86+
);
87+
```
88+
## React Q&A
89+
### Error "Module not found: Error: Can't resolve 'LIBRARY' in webpack 5"
90+
As webpack 5 no longer polyfills many Node APIs. We would need resolve the packages when upgrading from webpack 4 to webpack 5.
91+
You can resolve the issue by config the `webpack.config.js`.
92+
Example of resolve library "url" and "util":
93+
```
94+
resolve:{
95+
fallback:
96+
{
97+
"url": require.resolve("url/"),
98+
"util": require.resolve("util/")
99+
}
100+
}
101+
```
102+
The article could be helpful: https://gist.github.com/ef4/d2cf5672a93cf241fd47c020b9b3066a
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="theme-color" content="#000000" />
8+
<meta
9+
name="description"
10+
content="Web site created using create-react-app"
11+
/>
12+
<title>PubSub App</title>
13+
</head>
14+
<body>
15+
<div id="react_app"></div>
16+
<!--
17+
This HTML file is a template.
18+
If you open it directly in the browser, you will see an empty page.
19+
20+
You can add webfonts, meta tags, or analytics to this file.
21+
The build step will place the bundled scripts into the <body> tag.
22+
23+
To begin the development, run `npm start` or `yarn start`.
24+
To create a production bundle, use `npm run build` or `yarn build`.
25+
-->
26+
</body>
27+
</html>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "react_sample",
3+
"version": "1.0.0",
4+
"description": "Publish/Subscribe sample with React for AWS IoT Browser SDK",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"start": "webpack serve --port 3000",
9+
"build": "NODE_ENV=production webpack"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/aws/aws-iot-device-sdk-js-v2.git"
14+
},
15+
"author": "AWS SDK Common Runtime Team <[email protected]>",
16+
"license": "Apache-2.0",
17+
"bugs": {
18+
"url": "https://github.com/aws/aws-iot-device-sdk-js-v2/issues"
19+
},
20+
"homepage": "https://github.com/aws/aws-iot-device-sdk-js-v2#readme",
21+
"devDependencies": {
22+
"@types/jquery": "^3.5.16",
23+
"@types/react": "^18.2.6",
24+
"@types/react-dom": "^18.2.4",
25+
"babel-loader": "^9.1.2",
26+
"css-loader": "^6.7.4",
27+
"html-webpack-plugin": "^5.5.1",
28+
"mini-css-extract-plugin": "^2.7.6",
29+
"style-loader": "^3.3.3",
30+
"ts-loader": "^9.4.2",
31+
"typescript": "^5.0.4",
32+
"webpack": "^5.83.1",
33+
"webpack-cli": "^5.1.1",
34+
"webpack-dev-server": "^4.15.0"
35+
},
36+
"dependencies": {
37+
"@aws-sdk/credential-providers": "^3.226.0",
38+
"aws-iot-device-sdk-v2": "file:../../..",
39+
"aws-sdk": "^2.585.0",
40+
"events": "^3.3.0",
41+
"jquery": "^3.5.0",
42+
"react": "^18.2.0",
43+
"react-dom": "^18.2.0",
44+
"url": "^0.11.0"
45+
}
46+
}

0 commit comments

Comments
 (0)