@@ -7,27 +7,29 @@ MQTT client for React Native application.
7
7
- Secure MQTT connection over TLS 1.2.
8
8
- Authentication of both of server and client by X.509 certificates.
9
9
- Certificates and a private key stored in a device specific key store.
10
- - [ Android KeyStore] ( https://developer.android.com/training/articles/keystore#UsingAndroidKeyStore ) on Android
11
- - [ Default keychain] ( https://developer.apple.com/documentation/security/keychain_services/keychains ) on iOS
10
+ - [ Android KeyStore] ( https://developer.android.com/training/articles/keystore#UsingAndroidKeyStore ) on Android
11
+ - [ Default keychain] ( https://developer.apple.com/documentation/security/keychain_services/keychains ) on iOS
12
12
13
13
## Dependencies
14
14
15
15
This library wraps the following libraries,
16
+
16
17
- [ Paho MQTT Client for Android variant maintained by hannesa2] ( https://github.com/hannesa2/paho.mqtt.android ) (Android)
17
18
18
19
This library is forked as [ emoto-kc-ak/paho.mqtt.android] ( https://github.com/emoto-kc-ak/paho.mqtt.android ) to make Maven artifacts.
20
+
19
21
- [ CocoaMQTT] ( https://github.com/emqx/CocoaMQTT ) (iOS)
20
22
21
23
## Installation
22
24
23
25
``` sh
24
- npm install --save git+https://github.com/emoto-kc-ak/react-native-mqtt-client.git#v0.1.1
26
+ npm install --save git+https://github.com/emoto-kc-ak/react-native-mqtt-client.git#v0.1.4
25
27
```
26
28
27
29
## Usage
28
30
29
31
``` js
30
- import MqttClient from " react-native-mqtt-client" ;
32
+ import MqttClient from ' react-native-mqtt-client' ;
31
33
```
32
34
33
35
### Configuring an identity
@@ -43,13 +45,18 @@ MqttClient.setIdentity({
43
45
keyPem: IOT_KEY , // PEM representation string of a private key
44
46
keyStoreOptions, // options for a device specific key store. may be omitted
45
47
})
46
- .then (() => { /* handle success */ })
47
- .catch (({code, message}) => { /* handle error */ });
48
+ .then (() => {
49
+ /* handle success */
50
+ })
51
+ .catch (({ code, message }) => {
52
+ /* handle error */
53
+ });
48
54
```
49
55
50
56
` keyStoreOptions ` is an optional object that may have the following fields,
51
- - ` caCertAlias ` : (string) Alias associated with a root certificate (Android only). See [ ` KeyStore.setCertificateEntry ` ] ( https://developer.android.com/reference/java/security/KeyStore#setCertificateEntry(java.lang.String,%20java.security.cert.Certificate) )
52
- - ` keyAlias ` : (string) Alias associated with a private key (Android only). See [ ` KeyStore.setKeyEntry ` ] ( https://developer.android.com/reference/java/security/KeyStore#setKeyEntry(java.lang.String,%20java.security.Key,%20char[],%20java.security.cert.Certificate[]) )
57
+
58
+ - ` caCertAlias ` : (string) Alias associated with a root certificate (Android only). See [ ` KeyStore.setCertificateEntry ` ] ( < https://developer.android.com/reference/java/security/KeyStore#setCertificateEntry(java.lang.String,%20java.security.cert.Certificate) > )
59
+ - ` keyAlias ` : (string) Alias associated with a private key (Android only). See [ ` KeyStore.setKeyEntry ` ] ( < https://developer.android.com/reference/java/security/KeyStore#setKeyEntry(java.lang.String,%20java.security.Key,%20char[],%20java.security.cert.Certificate[]) > )
53
60
- ` caCertLabel ` : (string) Label associated with a root certificate (iOS only). See [ ` kSecAttrLabel ` ] ( https://developer.apple.com/documentation/security/ksecattrlabel )
54
61
- ` certLabel ` : (string) Label associated with a client certificate (iOS only). See [ ` kSecAttrLabel ` ] ( https://developer.apple.com/documentation/security/ksecattrlabel )
55
62
- ` keyApplicationTag ` : (string) Tag associated with a private key (iOS only). See [ ` kSecAttrApplicationTag ` ] ( https://developer.apple.com/documentation/security/ksecattrapplicationtag )
@@ -64,8 +71,12 @@ MqttClient.connect({
64
71
port: IOT_PORT , // (number) Port to connect.
65
72
clientId: IOT_DEVICE_ID , // (string) Client ID of a device connecting.
66
73
})
67
- .then (() => { /* handle success */ })
68
- .catch (({code, message}) => { /* handle error */ });
74
+ .then (() => {
75
+ /* handle success */
76
+ })
77
+ .catch (({ code, message }) => {
78
+ /* handle error */
79
+ });
69
80
```
70
81
71
82
It attempts to connect to ` ssl://$IOT_ENDPOINT:$IOT_PORT ` .
@@ -76,11 +87,16 @@ It attempts to connect to `ssl://$IOT_ENDPOINT:$IOT_PORT`.
76
87
77
88
``` js
78
89
MqttClient .publish (topic, payload)
79
- .then (() => { /* handle success */ })
80
- .catch (({code, message}) => { /* handle error */ });
90
+ .then (() => {
91
+ /* handle success */
92
+ })
93
+ .catch (({ code, message }) => {
94
+ /* handle error */
95
+ });
81
96
```
82
97
83
98
Where,
99
+
84
100
- ` topic ` : (string) Topic where ` payload ` is to be published.
85
101
- ` payload ` : (string) Payload to be published. Usually a stringified JSON object.
86
102
@@ -90,11 +106,16 @@ Where,
90
106
91
107
``` js
92
108
MqttClient .subscribe (topic)
93
- .then (() => { /* handle success */ })
94
- .catch (({code, message}) => { /* handle error */ });
109
+ .then (() => {
110
+ /* handle success */
111
+ })
112
+ .catch (({ code, message }) => {
113
+ /* handle error */
114
+ });
95
115
```
96
116
97
117
Where,
118
+
98
119
- ` topic ` : (string) Topic to subscribe.
99
120
100
121
To handle messages in the subscribed topic, you have to handle a [ ` receive-message ` event] ( #received-message ) .
@@ -104,7 +125,7 @@ To handle messages in the subscribed topic, you have to handle a [`receive-messa
104
125
` MqttClient.disconnect ` disconnects from an MQTT broker.
105
126
106
127
``` js
107
- MqttClient .disconnect ()
128
+ MqttClient .disconnect ();
108
129
```
109
130
110
131
### Loading an identity
@@ -113,8 +134,12 @@ An identity stored in a device specific key store by `MqttClient.setIdentity` ma
113
134
114
135
``` js
115
136
MqttClient .loadIdentity (keyStoreOptions)
116
- .then (() => { /* handle success */ })
117
- .catch (({code, message}) => { /* handle error */ });
137
+ .then (() => {
138
+ /* handle success */
139
+ })
140
+ .catch (({ code, message }) => {
141
+ /* handle error */
142
+ });
118
143
```
119
144
120
145
Please refer to [ Configuring an identity] ( #configuring-an-identity ) for details of ` keyStoreOptions ` .
@@ -125,8 +150,12 @@ An identity stored in a device specific key store by `MqttClient.setIdentity` ma
125
150
126
151
``` js
127
152
MqttClient .resetIdentity (keyStoreOptions)
128
- .then (() => { /* handle success */ })
129
- .catch (({code, message}) => { /* handle error */ });
153
+ .then (() => {
154
+ /* handle success */
155
+ })
156
+ .catch (({ code, message }) => {
157
+ /* handle error */
158
+ });
130
159
```
131
160
132
161
Please refer to [ Configuring an identity] ( #configuring-an-identity ) for details of ` keyStoreOptions ` .
@@ -137,11 +166,16 @@ Please refer to [Configuring an identity](#configuring-an-identity) for details
137
166
138
167
``` js
139
168
MqttClient .isIdentityStored (keyStoreOptions)
140
- .then ((isStored ) => { /* handle success */ })
141
- .catch (({code, message}) => { /* handle error */ });
169
+ .then ((isStored ) => {
170
+ /* handle success */
171
+ })
172
+ .catch (({ code, message }) => {
173
+ /* handle error */
174
+ });
142
175
```
143
176
144
177
Where,
178
+
145
179
- ` isStored ` : (boolean) Whether an identity is stored in a device-specific key store.
146
180
147
181
Please refer to [ Configuring an identity] ( #configuring-an-identity ) for details of ` keyStoreOptions ` .
@@ -155,26 +189,33 @@ Please refer to [Configuring an identity](#configuring-an-identity) for details
155
189
A ` connected ` event is notified when connection to an MQTT broker is established.
156
190
157
191
``` js
158
- MqttClient .addListener (' connected' , () => { /* handle connection */ })
192
+ MqttClient .addListener (' connected' , () => {
193
+ /* handle connection */
194
+ });
159
195
```
160
196
161
197
#### disconnected
162
198
163
199
A ` disconnected ` event is notified when connection to an MQTT broker is disconnected.
164
200
165
201
``` js
166
- MqttClient .addListener (' disconnected' , () => { /* handle disconnection */ })
202
+ MqttClient .addListener (' disconnected' , () => {
203
+ /* handle disconnection */
204
+ });
167
205
```
168
206
169
207
#### received-message
170
208
171
209
A ` received-message ` event is notified when a message is arrived from an MQTT broker.
172
210
173
211
``` js
174
- MqttClient .addListener (' received-message' , ({topic, payload}) => { /* handle message */ });
212
+ MqttClient .addListener (' received-message' , ({ topic, payload }) => {
213
+ /* handle message */
214
+ });
175
215
```
176
216
177
217
Where,
218
+
178
219
- ` topic ` : (string) Topic where a message has been published.
179
220
- ` payload ` : (string) Payload of a message. Usually a stringified JSON object.
180
221
@@ -183,7 +224,9 @@ Where,
183
224
A ` got-error ` event is notified when an error has occurred.
184
225
185
226
``` js
186
- MqttClient .addListener (' got-error' , (err ) => { /* handle error */ })
227
+ MqttClient .addListener (' got-error' , (err ) => {
228
+ /* handle error */
229
+ });
187
230
```
188
231
189
232
## iOS Tips
@@ -215,6 +258,7 @@ npm run prepare
215
258
```
216
259
217
260
Artifacts will be updated in the following directories,
261
+
218
262
- ` lib/commonjs `
219
263
- ` lib/module `
220
264
- ` lib/typescript `
229
273
230
274
## Acknowlegement
231
275
232
- This project is bootstrapped with [ callstack/react-native-builder-bob] ( https://github.com/callstack/react-native-builder-bob ) .
276
+ This project is bootstrapped with [ callstack/react-native-builder-bob] ( https://github.com/callstack/react-native-builder-bob ) .
0 commit comments