mqtt 4.0.0 → 4.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/README.md +58 -24
  2. package/bin/mqtt.js +27 -0
  3. package/bin/pub.js +0 -0
  4. package/bin/sub.js +0 -0
  5. package/dist/mqtt.js +2794 -2512
  6. package/dist/mqtt.min.js +1 -1
  7. package/lib/client.js +72 -59
  8. package/lib/connect/index.js +7 -2
  9. package/lib/connect/tcp.js +2 -0
  10. package/lib/connect/tls.js +3 -0
  11. package/lib/connect/ws.js +194 -34
  12. package/lib/connect/wx.js +3 -3
  13. package/lib/store.js +2 -2
  14. package/mqtt.js +0 -24
  15. package/package.json +9 -10
  16. package/types/lib/client-options.d.ts +14 -1
  17. package/types/lib/client.d.ts +4 -3
  18. package/examples/client/secure-client.js +0 -24
  19. package/examples/client/simple-both.js +0 -13
  20. package/examples/client/simple-publish.js +0 -7
  21. package/examples/client/simple-subscribe.js +0 -9
  22. package/examples/tls client/crt.ca.cg.pem +0 -34
  23. package/examples/tls client/mqttclient.js +0 -48
  24. package/examples/tls client/tls-cert.pem +0 -13
  25. package/examples/tls client/tls-key.pem +0 -15
  26. package/examples/wss/client.js +0 -49
  27. package/examples/wss/client_with_proxy.js +0 -58
  28. package/test/abstract_client.js +0 -3139
  29. package/test/abstract_store.js +0 -135
  30. package/test/browser/server.js +0 -132
  31. package/test/browser/test.js +0 -92
  32. package/test/client.js +0 -540
  33. package/test/client_mqtt5.js +0 -537
  34. package/test/helpers/port_list.js +0 -45
  35. package/test/helpers/private-csr.pem +0 -16
  36. package/test/helpers/private-key.pem +0 -27
  37. package/test/helpers/public-cert.pem +0 -19
  38. package/test/helpers/public-key.pem +0 -9
  39. package/test/helpers/server.js +0 -53
  40. package/test/helpers/server_process.js +0 -9
  41. package/test/helpers/tls-cert.pem +0 -14
  42. package/test/helpers/tls-csr.pem +0 -11
  43. package/test/helpers/tls-key.pem +0 -15
  44. package/test/helpers/wrong-cert.pem +0 -13
  45. package/test/helpers/wrong-csr.pem +0 -11
  46. package/test/helpers/wrong-key.pem +0 -15
  47. package/test/mocha.opts +0 -4
  48. package/test/mqtt.js +0 -230
  49. package/test/mqtt_store.js +0 -9
  50. package/test/secure_client.js +0 -188
  51. package/test/server.js +0 -94
  52. package/test/server_helpers_for_client_tests.js +0 -100
  53. package/test/store.js +0 -10
  54. package/test/typescript/broker-connect-subscribe-and-publish.js +0 -24
  55. package/test/typescript/broker-connect-subscribe-and-publish.js.map +0 -1
  56. package/test/typescript/broker-connect-subscribe-and-publish.ts +0 -25
  57. package/test/typescript/tsconfig.json +0 -14
  58. package/test/util.js +0 -13
  59. package/test/websocket_client.js +0 -146
package/README.md CHANGED
@@ -151,6 +151,63 @@ $env:DEBUG='mqttjs*'
151
151
 
152
152
  ```
153
153
 
154
+ <a name="reconnecting"></a>
155
+ ## About Reconnection
156
+
157
+ An important part of any websocket connection is what to do when a connection
158
+ drops off and the client needs to reconnect. MQTT has built-in reconnection
159
+ support that can be configured to behave in ways that suit the application.
160
+
161
+ #### Refresh Authentication Options / Signed Urls with `transformWsUrl` (Websocket Only)
162
+
163
+ When an mqtt connection drops and needs to reconnect, it's common to require
164
+ that any authentication associated with the connection is kept current with
165
+ the underlying auth mechanism. For instance some applications may pass an auth
166
+ token with connection options on the initial connection, while other cloud
167
+ services may require a url be signed with each connection.
168
+
169
+ By the time the reconnect happens in the application lifecycle, the original
170
+ auth data may have expired.
171
+
172
+ To address this we can use a hook called `transformWsUrl` to manipulate
173
+ either of the connection url or the client options at the time of a reconnect.
174
+
175
+ Example (update clientId & username on each reconnect):
176
+ ```
177
+ const transformWsUrl = (url, options, client) => {
178
+ client.options.username = `token=${this.get_current_auth_token()}`;
179
+ client.options.clientId = `${this.get_updated_clientId()}`;
180
+
181
+ return `${this.get_signed_cloud_url(url)`;
182
+ }
183
+
184
+ const connection = await mqtt.connectAsync(<wss url>, {
185
+ ...,
186
+ transformWsUrl: transformUrl,
187
+ });
188
+
189
+ ```
190
+ Now every time a new WebSocket connection is opened (hopefully not too often),
191
+ we will get a fresh signed url or fresh auth token data.
192
+
193
+ Note: Currently this hook does _not_ support promises, meaning that in order to
194
+ use the latest auth token, you must have some outside mechanism running that
195
+ handles application-level authentication refreshing so that the websocket
196
+ connection can simply grab the latest valid token or signed url.
197
+
198
+
199
+ #### Enabling Reconnection with `reconnectPeriod` option
200
+
201
+ To ensure that the mqtt client automatically tries to reconnect when the
202
+ connection is dropped, you must set the client option `reconnectPeriod` to a
203
+ value greater than 0. A value of 0 will disable reconnection and then terminate
204
+ the final connection when it drops.
205
+
206
+ The default value is 1000 ms which means it will try to reconnect 1 second
207
+ after losing the connection.
208
+
209
+
210
+
154
211
  <a name="api"></a>
155
212
  ## API
156
213
 
@@ -222,7 +279,7 @@ the `connect` event. Typically a `net.Socket`.
222
279
  * `clean`: `true`, set to false to receive QoS 1 and 2 messages while
223
280
  offline
224
281
  * `reconnectPeriod`: `1000` milliseconds, interval between two
225
- reconnections
282
+ reconnections. Disable auto reconnect by setting to `0`.
226
283
  * `connectTimeout`: `30 * 1000` milliseconds, time to wait before a
227
284
  CONNACK is received
228
285
  * `username`: the username required by your broker, if any
@@ -641,29 +698,6 @@ you can then use mqtt.js in the browser with the same api than node's one.
641
698
 
642
699
  Your broker should accept websocket connection (see [MQTT over Websockets](https://github.com/mcollina/mosca/wiki/MQTT-over-Websockets) to setup [Mosca](http://mcollina.github.io/mosca/)).
643
700
 
644
- <a name="signedurls"></a>
645
- ### Signed WebSocket Urls
646
-
647
- If you need to sign an url, for example for [AWS IoT](http://docs.aws.amazon.com/iot/latest/developerguide/protocols.html#mqtt-ws),
648
- then you can pass in a `transformWsUrl` function to the <a href="#connect"><code>mqtt.<b>connect()</b></code></a> options
649
- This is needed because signed urls have an expiry and eventually upon reconnects, a new signed url needs to be created:
650
-
651
- ```js
652
- // This module doesn't actually exist, just an example
653
- var awsIotUrlSigner = require('awsIotUrlSigner')
654
- mqtt.connect('wss://a2ukbzaqo9vbpb.iot.ap-southeast-1.amazonaws.com/mqtt', {
655
- transformWsUrl: function (url, options, client) {
656
- // It's possible to inspect some state on options(pre parsed url components)
657
- // and the client (reconnect state etc)
658
- return awsIotUrlSigner(url)
659
- }
660
- })
661
-
662
- // Now every time a new WebSocket connection is opened (hopefully not that
663
- // often) we get a freshly signed url
664
-
665
- ```
666
-
667
701
  <a name="qos"></a>
668
702
  ## About QoS
669
703
 
package/bin/mqtt.js ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env node
2
+ 'use strict'
3
+
4
+ /*
5
+ * Copyright (c) 2015-2015 MQTT.js contributors.
6
+ * Copyright (c) 2011-2014 Adam Rudd.
7
+ *
8
+ * See LICENSE for more information
9
+ */
10
+ var path = require('path')
11
+ var commist = require('commist')()
12
+ var helpMe = require('help-me')({
13
+ dir: path.join(path.dirname(require.main.filename), '/../doc'),
14
+ ext: '.txt'
15
+ })
16
+
17
+ commist.register('publish', require('./pub'))
18
+ commist.register('subscribe', require('./sub'))
19
+ commist.register('version', function () {
20
+ console.log('MQTT.js version:', require('./../package.json').version)
21
+ })
22
+ commist.register('help', helpMe.toStdout)
23
+
24
+ if (commist.parse(process.argv.slice(2)) !== null) {
25
+ console.log('No such command:', process.argv[2], '\n')
26
+ helpMe.toStdout()
27
+ }
package/bin/pub.js CHANGED
File without changes
package/bin/sub.js CHANGED
File without changes