mqtt 4.2.5 → 4.3.0
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.
- package/CHANGELOG.md +63 -0
- package/README.md +108 -49
- package/bin/mqtt.js +3 -3
- package/bin/pub.js +12 -12
- package/bin/sub.js +6 -6
- package/dist/mqtt.js +13015 -5878
- package/dist/mqtt.min.js +1 -1
- package/lib/client.js +569 -260
- package/lib/connect/ali.js +14 -14
- package/lib/connect/index.js +18 -30
- package/lib/connect/tcp.js +4 -5
- package/lib/connect/tls.js +8 -5
- package/lib/connect/ws.js +16 -15
- package/lib/connect/wx.js +11 -13
- package/lib/default-message-id-provider.js +69 -0
- package/lib/store.js +9 -9
- package/lib/topic-alias-recv.js +47 -0
- package/lib/topic-alias-send.js +93 -0
- package/lib/unique-message-id-provider.js +65 -0
- package/lib/validations.js +3 -3
- package/mqtt.js +7 -3
- package/package.json +26 -10
- package/types/index.d.ts +4 -1
- package/types/lib/client-options.d.ts +24 -11
- package/types/lib/client.d.ts +33 -4
- package/types/lib/default-message-id-provider.d.ts +49 -0
- package/types/lib/message-id-provider.d.ts +40 -0
- package/types/lib/unique-message-id-provider.d.ts +48 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Release History
|
|
2
|
+
|
|
3
|
+
## 4.3.0
|
|
4
|
+
|
|
5
|
+
### PR
|
|
6
|
+
|
|
7
|
+
refined topic alias support (#1301)
|
|
8
|
+
|
|
9
|
+
fix security vulnerability in ws stream (#1307)
|
|
10
|
+
|
|
11
|
+
skip TLS SNI if host is IP address (#1311)
|
|
12
|
+
|
|
13
|
+
update readme about vNext discussions (#1328)
|
|
14
|
+
|
|
15
|
+
update readme sample (#1331)
|
|
16
|
+
|
|
17
|
+
add support for ALPN TLS extension (#1332)
|
|
18
|
+
|
|
19
|
+
align onConnectCallback with specs expecting connack packet (#1333)
|
|
20
|
+
|
|
21
|
+
fix resubscribe messageId allocate twice (#1337)
|
|
22
|
+
|
|
23
|
+
rework examples to be a bit more specific (#1352)
|
|
24
|
+
|
|
25
|
+
readme typo fixed (#1353)
|
|
26
|
+
|
|
27
|
+
fix(typescript): use correct version of @types/ws (#1358)
|
|
28
|
+
|
|
29
|
+
fix(type): fix push properties types (#1359)
|
|
30
|
+
|
|
31
|
+
fix: audit dev dependencies (#1374)
|
|
32
|
+
|
|
33
|
+
fix(type): add properties type for IClientSubscribeOptions (#1378)
|
|
34
|
+
|
|
35
|
+
feat(client): auth handler for enhanced auth (#1380)
|
|
36
|
+
|
|
37
|
+
## 4.2.8
|
|
38
|
+
|
|
39
|
+
### PR
|
|
40
|
+
|
|
41
|
+
Fix ws vulnerability and typescript bug (#1292)
|
|
42
|
+
|
|
43
|
+
## 4.2.7
|
|
44
|
+
|
|
45
|
+
### PR
|
|
46
|
+
|
|
47
|
+
#1287 - Fix production vulnerabilities (#1289)
|
|
48
|
+
|
|
49
|
+
#1215 - Add missing 'duplexify' dependency (#1266)
|
|
50
|
+
|
|
51
|
+
Improve type definition for 'wsOptions' (#1256)
|
|
52
|
+
|
|
53
|
+
Improve Typescript Declaratiosn for userProperties (#1249)
|
|
54
|
+
|
|
55
|
+
#1235 - Call the end on the WebSocket stream when WebSocket close event is emitted. (#1239)
|
|
56
|
+
|
|
57
|
+
#1201 - Uncaught TypeError: net.createConnection is not a function. (#1236)
|
|
58
|
+
|
|
59
|
+
Improve Documentation for Browserify (#1224)
|
|
60
|
+
|
|
61
|
+
## v4.2.6 and Below
|
|
62
|
+
|
|
63
|
+
The release history has beend documented in the GitHub releases and tags historically.
|
package/README.md
CHANGED
|
@@ -6,15 +6,18 @@
|
|
|
6
6
|
MQTT.js is a client library for the [MQTT](http://mqtt.org/) protocol, written
|
|
7
7
|
in JavaScript for node.js and the browser.
|
|
8
8
|
|
|
9
|
+
## Table of Contents
|
|
10
|
+
* [__MQTT.js vNext__](#vnext)
|
|
9
11
|
* [Upgrade notes](#notes)
|
|
10
12
|
* [Installation](#install)
|
|
11
13
|
* [Example](#example)
|
|
14
|
+
* [Import Styles](#example)
|
|
12
15
|
* [Command Line Tools](#cli)
|
|
13
16
|
* [API](#api)
|
|
14
17
|
* [Browser](#browser)
|
|
15
|
-
* [Weapp](#weapp)
|
|
16
18
|
* [About QoS](#qos)
|
|
17
19
|
* [TypeScript](#typescript)
|
|
20
|
+
* [Weapp and Ali support](#weapp-alipay)
|
|
18
21
|
* [Contributing](#contributing)
|
|
19
22
|
* [License](#license)
|
|
20
23
|
|
|
@@ -23,6 +26,9 @@ MQTT.js is an OPEN Open Source Project, see the [Contributing](#contributing) se
|
|
|
23
26
|
[](https://github.com/feross/standard)
|
|
25
28
|
|
|
29
|
+
<a name="vnext"></a>
|
|
30
|
+
## Discussion on the next major version of MQTT.js
|
|
31
|
+
There are discussions happening on the future of MQTT.js and the next major version (vNext). We invite the community to provide their thoughts and feedback in [this GitHub discussion](https://github.com/mqttjs/MQTT.js/discussions/1324)
|
|
26
32
|
|
|
27
33
|
<a name="notes"></a>
|
|
28
34
|
## Important notes for existing users
|
|
@@ -77,8 +83,8 @@ npm install mqtt --save
|
|
|
77
83
|
For the sake of simplicity, let's put the subscriber and the publisher in the same file:
|
|
78
84
|
|
|
79
85
|
```js
|
|
80
|
-
|
|
81
|
-
|
|
86
|
+
const mqtt = require('mqtt')
|
|
87
|
+
const client = mqtt.connect('mqtt://test.mosquitto.org')
|
|
82
88
|
|
|
83
89
|
client.on('connect', function () {
|
|
84
90
|
client.subscribe('presence', function (err) {
|
|
@@ -111,6 +117,25 @@ If you do not want to install a separate broker, you can try using the
|
|
|
111
117
|
|
|
112
118
|
to use MQTT.js in the browser see the [browserify](#browserify) section
|
|
113
119
|
|
|
120
|
+
<a name="import_styles"></a>
|
|
121
|
+
## Import styles
|
|
122
|
+
### CommonJS (Require)
|
|
123
|
+
```js
|
|
124
|
+
const mqtt = require('mqtt') // require mqtt
|
|
125
|
+
const client = mqtt.connect('est.mosquitto.org') // create a client
|
|
126
|
+
```
|
|
127
|
+
### ES6 Modules (Import)
|
|
128
|
+
#### Aliased wildcard import
|
|
129
|
+
```js
|
|
130
|
+
import * as mqtt from "mqtt" // import everything inside the mqtt module and give it the namespace "mqtt"
|
|
131
|
+
let client = mqtt.connect('mqtt://test.mosquitto.org') // create a client
|
|
132
|
+
```
|
|
133
|
+
#### Importing individual components
|
|
134
|
+
```js
|
|
135
|
+
import { connect } from "mqtt" // import connect from mqtt
|
|
136
|
+
let client = connect('mqtt://test.mosquitto.org') // create a client
|
|
137
|
+
```
|
|
138
|
+
|
|
114
139
|
<a name="promises"></a>
|
|
115
140
|
## Promise support
|
|
116
141
|
|
|
@@ -206,7 +231,41 @@ the final connection when it drops.
|
|
|
206
231
|
The default value is 1000 ms which means it will try to reconnect 1 second
|
|
207
232
|
after losing the connection.
|
|
208
233
|
|
|
234
|
+
<a name="topicalias"></a>
|
|
235
|
+
## About Topic Alias Management
|
|
236
|
+
|
|
237
|
+
### Enabling automatic Topic Alias using
|
|
238
|
+
If the client sets the option `autoUseTopicAlias:true` then MQTT.js uses existing topic alias automatically.
|
|
239
|
+
|
|
240
|
+
example scenario:
|
|
241
|
+
```
|
|
242
|
+
1. PUBLISH topic:'t1', ta:1 (register)
|
|
243
|
+
2. PUBLISH topic:'t1' -> topic:'', ta:1 (auto use existing map entry)
|
|
244
|
+
3. PUBLISH topic:'t2', ta:1 (register overwrite)
|
|
245
|
+
4. PUBLISH topic:'t2' -> topic:'', ta:1 (auto use existing map entry based on the receent map)
|
|
246
|
+
5. PUBLISH topic:'t1' (t1 is no longer mapped to ta:1)
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
User doesn't need to manage which topic is mapped to which topic alias.
|
|
250
|
+
If the user want to register topic alias, then publish topic with topic alias.
|
|
251
|
+
If the user want to use topic alias, then publish topic without topic alias. If there is a mapped topic alias then added it as a property and update the topic to empty string.
|
|
252
|
+
|
|
253
|
+
### Enabling automatic Topic Alias assign
|
|
209
254
|
|
|
255
|
+
If the client sets the option `autoAssignTopicAlias:true` then MQTT.js uses existing topic alias automatically.
|
|
256
|
+
If no topic alias exists, then assign a new vacant topic alias automatically. If topic alias is fully used, then LRU(Least Recently Used) topic-alias entry is overwritten.
|
|
257
|
+
|
|
258
|
+
example scenario:
|
|
259
|
+
```
|
|
260
|
+
The broker returns CONNACK (TopicAliasMaximum:3)
|
|
261
|
+
1. PUBLISH topic:'t1' -> 't1', ta:1 (auto assign t1:1 and register)
|
|
262
|
+
2. PUBLISH topic:'t1' -> '' , ta:1 (auto use existing map entry)
|
|
263
|
+
3. PUBLISH topic:'t2' -> 't2', ta:2 (auto assign t1:2 and register. 2 was vacant)
|
|
264
|
+
4. PUBLISH topic:'t3' -> 't3', ta:3 (auto assign t1:3 and register. 3 was vacant)
|
|
265
|
+
5. PUBLISH topic:'t4' -> 't4', ta:1 (LRU entry is overwritten)
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
Also user can manually register topic-alias pair using PUBLISH topic:'some', ta:X. It works well with automatic topic alias assign.
|
|
210
269
|
|
|
211
270
|
<a name="api"></a>
|
|
212
271
|
## API
|
|
@@ -237,7 +296,7 @@ Connects to the broker specified by the given url and options and
|
|
|
237
296
|
returns a [Client](#client).
|
|
238
297
|
|
|
239
298
|
The URL can be on the following protocols: 'mqtt', 'mqtts', 'tcp',
|
|
240
|
-
'tls', 'ws', 'wss'. The URL can also be an object as returned by
|
|
299
|
+
'tls', 'ws', 'wss', 'wxs', 'alis'. The URL can also be an object as returned by
|
|
241
300
|
[`URL.parse()`](http://nodejs.org/api/url.html#url_url_parse_urlstr_parsequerystring_slashesdenotehost),
|
|
242
301
|
in that case the two objects are merged, i.e. you can pass a single
|
|
243
302
|
object with both the URL and the connect options.
|
|
@@ -291,6 +350,8 @@ the `connect` event. Typically a `net.Socket`.
|
|
|
291
350
|
```js
|
|
292
351
|
customHandleAcks: function(topic, message, packet, done) {/*some logic wit colling done(error, reasonCode)*/}
|
|
293
352
|
```
|
|
353
|
+
* `autoUseTopicAlias`: enabling automatic Topic Alias using functionality
|
|
354
|
+
* `autoAssignTopicAlias`: enabling automatic Topic Alias assign functionality
|
|
294
355
|
* `properties`: properties MQTT 5.0.
|
|
295
356
|
`object` that supports the following properties:
|
|
296
357
|
* `sessionExpiryInterval`: representing the Session Expiry Interval in seconds `number`,
|
|
@@ -322,6 +383,7 @@ the `connect` event. Typically a `net.Socket`.
|
|
|
322
383
|
urls which upon reconnect can have become expired.
|
|
323
384
|
* `resubscribe` : if connection is broken and reconnects,
|
|
324
385
|
subscribed topics are automatically subscribed again (default `true`)
|
|
386
|
+
* `messageIdProvider`: custom messageId provider. when `new UniqueMessageIdProvider()` is set, then non conflict messageId is provided.
|
|
325
387
|
|
|
326
388
|
In case mqtts (mqtt over tls) is required, the `options` object is
|
|
327
389
|
passed through to
|
|
@@ -610,59 +672,30 @@ The MQTT.js bundle is available through http://unpkg.com, specifically
|
|
|
610
672
|
at https://unpkg.com/mqtt/dist/mqtt.min.js.
|
|
611
673
|
See http://unpkg.com for the full documentation on version ranges.
|
|
612
674
|
|
|
613
|
-
<a name="weapp"></a>
|
|
614
|
-
## WeChat Mini Program
|
|
615
|
-
Support [WeChat Mini Program](https://mp.weixin.qq.com/). See [Doc](https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-socket.html).
|
|
616
|
-
<a name="example"></a>
|
|
617
|
-
|
|
618
|
-
## Example(js)
|
|
619
|
-
|
|
620
|
-
```js
|
|
621
|
-
var mqtt = require('mqtt')
|
|
622
|
-
var client = mqtt.connect('wxs://test.mosquitto.org')
|
|
623
|
-
```
|
|
624
|
-
|
|
625
|
-
## Example(ts)
|
|
626
|
-
|
|
627
|
-
```ts
|
|
628
|
-
import { connect } from 'mqtt';
|
|
629
|
-
const client = connect('wxs://test.mosquitto.org');
|
|
630
|
-
```
|
|
631
|
-
|
|
632
|
-
## Ali Mini Program
|
|
633
|
-
Surport [Ali Mini Program](https://open.alipay.com/channel/miniIndex.htm). See [Doc](https://docs.alipay.com/mini/developer/getting-started).
|
|
634
|
-
<a name="example"></a>
|
|
635
|
-
|
|
636
|
-
## Example(js)
|
|
637
|
-
|
|
638
|
-
```js
|
|
639
|
-
var mqtt = require('mqtt')
|
|
640
|
-
var client = mqtt.connect('alis://test.mosquitto.org')
|
|
641
|
-
```
|
|
642
|
-
|
|
643
|
-
## Example(ts)
|
|
644
|
-
|
|
645
|
-
```ts
|
|
646
|
-
import { connect } from 'mqtt';
|
|
647
|
-
const client = connect('alis://test.mosquitto.org');
|
|
648
|
-
```
|
|
649
|
-
|
|
650
675
|
<a name="browserify"></a>
|
|
651
676
|
### Browserify
|
|
652
677
|
|
|
653
678
|
In order to use MQTT.js as a browserify module you can either require it in your browserify bundles or build it as a stand alone module. The exported module is AMD/CommonJs compatible and it will add an object in the global space.
|
|
654
679
|
|
|
655
|
-
```
|
|
656
|
-
|
|
657
|
-
cd
|
|
658
|
-
npm install
|
|
659
|
-
|
|
680
|
+
```bash
|
|
681
|
+
mkdir tmpdir
|
|
682
|
+
cd tmpdir
|
|
683
|
+
npm install mqtt
|
|
684
|
+
npm install browserify
|
|
685
|
+
npm install tinyify
|
|
686
|
+
cd node_modules/mqtt/
|
|
687
|
+
npm install .
|
|
688
|
+
npx browserify mqtt.js -s mqtt >browserMqtt.js // use script tag
|
|
689
|
+
# show size for compressed browser transfer
|
|
690
|
+
gzip <browserMqtt.js | wc -c
|
|
660
691
|
```
|
|
661
692
|
|
|
693
|
+
**Be sure to only use this bundle with `ws` or `wss` URLs in the browser. Others URL types will likey fail**
|
|
694
|
+
|
|
662
695
|
<a name="webpack"></a>
|
|
663
696
|
### Webpack
|
|
664
697
|
|
|
665
|
-
Just like browserify, export MQTT.js as library. The exported module would be `
|
|
698
|
+
Just like browserify, export MQTT.js as library. The exported module would be `const mqtt = xxx` and it will add an object in the global space. You could also export module in other [formats (AMD/CommonJS/others)](http://webpack.github.io/docs/configuration.html#output-librarytarget) by setting **output.libraryTarget** in webpack configuration.
|
|
666
699
|
|
|
667
700
|
```javascript
|
|
668
701
|
npm install -g webpack // install webpack
|
|
@@ -682,7 +715,7 @@ you can then use mqtt.js in the browser with the same api than node's one.
|
|
|
682
715
|
<body>
|
|
683
716
|
<script src="./browserMqtt.js"></script>
|
|
684
717
|
<script>
|
|
685
|
-
|
|
718
|
+
const client = mqtt.connect() // you add a ws:// url here
|
|
686
719
|
client.subscribe("mqtt/demo")
|
|
687
720
|
|
|
688
721
|
client.on("message", function (topic, payload) {
|
|
@@ -726,7 +759,7 @@ export default () => {
|
|
|
726
759
|
|
|
727
760
|
return (
|
|
728
761
|
<>
|
|
729
|
-
{
|
|
762
|
+
{messages.map((message) => (
|
|
730
763
|
<h2>{message}</h2>
|
|
731
764
|
)
|
|
732
765
|
</>
|
|
@@ -757,6 +790,31 @@ Before you can begin using these TypeScript definitions with your project, you n
|
|
|
757
790
|
* Set tsconfig.json: `{"compilerOptions" : {"moduleResolution" : "node"}, ...}`
|
|
758
791
|
* Includes the TypeScript definitions for node. You can use npm to install this by typing the following into a terminal window:
|
|
759
792
|
`npm install --save-dev @types/node`
|
|
793
|
+
|
|
794
|
+
### Typescript example
|
|
795
|
+
```
|
|
796
|
+
import * as mqtt from "mqtt"
|
|
797
|
+
let client : mqtt.MqttClient = mqtt.connect('mqtt://test.mosquitto.org')
|
|
798
|
+
```
|
|
799
|
+
|
|
800
|
+
<a name="weapp-alipay"></a>
|
|
801
|
+
## WeChat and Ali Mini Program support
|
|
802
|
+
### WeChat Mini Program
|
|
803
|
+
Supports [WeChat Mini Program](https://mp.weixin.qq.com/). Use the `wxs` protocol. See [the WeChat docs](https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-socket.html).
|
|
804
|
+
|
|
805
|
+
```js
|
|
806
|
+
const mqtt = require('mqtt')
|
|
807
|
+
const client = mqtt.connect('wxs://test.mosquitto.org')
|
|
808
|
+
```
|
|
809
|
+
|
|
810
|
+
### Ali Mini Program
|
|
811
|
+
Supports [Ali Mini Program](https://open.alipay.com/channel/miniIndex.htm). Use the `alis` protocol. See [the Alipay docs](https://docs.alipay.com/mini/developer/getting-started).
|
|
812
|
+
<a name="example"></a>
|
|
813
|
+
|
|
814
|
+
```js
|
|
815
|
+
const mqtt = require('mqtt')
|
|
816
|
+
const client = mqtt.connect('alis://test.mosquitto.org')
|
|
817
|
+
```
|
|
760
818
|
|
|
761
819
|
<a name="contributing"></a>
|
|
762
820
|
## Contributing
|
|
@@ -778,6 +836,7 @@ MQTT.js is only possible due to the excellent work of the following contributors
|
|
|
778
836
|
<tr><th align="left">Siarhei Buntsevich</th><td><a href="https://github.com/scarry1992">GitHub/scarry1992</a></td></tr>
|
|
779
837
|
</tbody></table>
|
|
780
838
|
|
|
839
|
+
|
|
781
840
|
<a name="license"></a>
|
|
782
841
|
## License
|
|
783
842
|
|
package/bin/mqtt.js
CHANGED
|
@@ -7,9 +7,9 @@
|
|
|
7
7
|
*
|
|
8
8
|
* See LICENSE for more information
|
|
9
9
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
const path = require('path')
|
|
11
|
+
const commist = require('commist')()
|
|
12
|
+
const helpMe = require('help-me')({
|
|
13
13
|
dir: path.join(path.dirname(require.main.filename), '/../doc'),
|
|
14
14
|
ext: '.txt'
|
|
15
15
|
})
|
package/bin/pub.js
CHANGED
|
@@ -2,20 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
'use strict'
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
5
|
+
const mqtt = require('../')
|
|
6
|
+
const pump = require('pump')
|
|
7
|
+
const path = require('path')
|
|
8
|
+
const fs = require('fs')
|
|
9
|
+
const concat = require('concat-stream')
|
|
10
|
+
const Writable = require('readable-stream').Writable
|
|
11
|
+
const helpMe = require('help-me')({
|
|
12
12
|
dir: path.join(__dirname, '..', 'doc')
|
|
13
13
|
})
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
const minimist = require('minimist')
|
|
15
|
+
const split2 = require('split2')
|
|
16
16
|
|
|
17
17
|
function send (args) {
|
|
18
|
-
|
|
18
|
+
const client = mqtt.connect(args)
|
|
19
19
|
client.on('connect', function () {
|
|
20
20
|
client.publish(args.topic, args.message, args, function (err) {
|
|
21
21
|
if (err) {
|
|
@@ -31,8 +31,8 @@ function send (args) {
|
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
function multisend (args) {
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
const client = mqtt.connect(args)
|
|
35
|
+
const sender = new Writable({
|
|
36
36
|
objectMode: true
|
|
37
37
|
})
|
|
38
38
|
sender._write = function (line, enc, cb) {
|
package/bin/sub.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
const mqtt = require('../')
|
|
4
|
+
const path = require('path')
|
|
5
|
+
const fs = require('fs')
|
|
6
|
+
const helpMe = require('help-me')({
|
|
7
7
|
dir: path.join(__dirname, '..', 'doc')
|
|
8
8
|
})
|
|
9
|
-
|
|
9
|
+
const minimist = require('minimist')
|
|
10
10
|
|
|
11
11
|
function start (args) {
|
|
12
12
|
args = minimist(args, {
|
|
@@ -84,7 +84,7 @@ function start (args) {
|
|
|
84
84
|
|
|
85
85
|
args.keepAlive = args['keep-alive']
|
|
86
86
|
|
|
87
|
-
|
|
87
|
+
const client = mqtt.connect(args)
|
|
88
88
|
|
|
89
89
|
client.on('connect', function () {
|
|
90
90
|
client.subscribe(args.topic, { qos: args.qos }, function (err, result) {
|