mqtt 5.0.0-beta.1 → 5.0.0-beta.3
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/README.md +44 -28
- package/bin/mqtt.js +7 -7
- package/bin/pub.js +139 -126
- package/bin/sub.js +127 -109
- package/dist/mqtt.js +5513 -4892
- package/dist/mqtt.min.js +4 -4
- package/lib/client.js +1642 -1884
- package/lib/connect/ali.js +100 -103
- package/lib/connect/index.js +135 -129
- package/lib/connect/tcp.js +7 -8
- package/lib/connect/tls.js +45 -41
- package/lib/connect/ws.js +240 -232
- package/lib/connect/wx.js +117 -118
- package/lib/default-message-id-provider.js +56 -62
- package/lib/handlers/ack.js +153 -0
- package/lib/handlers/auth.js +34 -0
- package/lib/handlers/connack.js +51 -0
- package/lib/handlers/index.js +66 -0
- package/lib/handlers/publish.js +167 -0
- package/lib/handlers/pubrel.js +24 -0
- package/lib/is-browser.js +5 -5
- package/lib/store.js +104 -111
- package/lib/topic-alias-recv.js +33 -36
- package/lib/topic-alias-send.js +71 -74
- package/lib/unique-message-id-provider.js +53 -57
- package/lib/validations.js +27 -29
- package/package.json +40 -45
- package/types/lib/client-options.d.ts +17 -1
- package/types/lib/client.d.ts +13 -1
package/README.md
CHANGED
|
@@ -2,10 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
 [](https://codecov.io/gh/mqttjs/MQTT.js)
|
|
4
4
|
|
|
5
|
+
[](http://standardjs.com/) [](https://github.com/mqttjs/MQTT.js/graphs/commit-activity)
|
|
6
|
+
[](https://github.com/mqttjs/MQTT.js/pulls)\
|
|
7
|
+
|
|
8
|
+
 
|
|
9
|
+
|
|
5
10
|
MQTT.js is a client library for the [MQTT](http://mqtt.org/) protocol, written
|
|
6
11
|
in JavaScript for node.js and the browser.
|
|
7
12
|
|
|
8
|
-
> MQTT [5.0.0
|
|
13
|
+
> MQTT [5.0.0 BETA](https://www.npmjs.com/package/mqtt/v/beta) is now available! Try it out and give us [feedback](https://github.com/mqttjs/MQTT.js/issues/1639): `npm i mqtt@beta`
|
|
9
14
|
|
|
10
15
|
## Table of Contents
|
|
11
16
|
|
|
@@ -96,15 +101,15 @@ For the sake of simplicity, let's put the subscriber and the publisher in the sa
|
|
|
96
101
|
const mqtt = require("mqtt");
|
|
97
102
|
const client = mqtt.connect("mqtt://test.mosquitto.org");
|
|
98
103
|
|
|
99
|
-
client.on("connect",
|
|
100
|
-
client.subscribe("presence",
|
|
104
|
+
client.on("connect", () => {
|
|
105
|
+
client.subscribe("presence", (err) => {
|
|
101
106
|
if (!err) {
|
|
102
107
|
client.publish("presence", "Hello mqtt");
|
|
103
108
|
}
|
|
104
109
|
});
|
|
105
110
|
});
|
|
106
111
|
|
|
107
|
-
client.on("message",
|
|
112
|
+
client.on("message", (topic, message) => {
|
|
108
113
|
// message is Buffer
|
|
109
114
|
console.log(message.toString());
|
|
110
115
|
client.end();
|
|
@@ -135,8 +140,8 @@ to use MQTT.js in the browser see the [browserify](#browserify) section
|
|
|
135
140
|
### CommonJS (Require)
|
|
136
141
|
|
|
137
142
|
```js
|
|
138
|
-
const mqtt = require("mqtt")
|
|
139
|
-
const client = mqtt.connect("
|
|
143
|
+
const mqtt = require("mqtt") // require mqtt
|
|
144
|
+
const client = mqtt.connect("test.mosquitto.org") // create a client
|
|
140
145
|
```
|
|
141
146
|
|
|
142
147
|
### ES6 Modules (Import)
|
|
@@ -300,23 +305,24 @@ Also user can manually register topic-alias pair using PUBLISH topic:'some', ta:
|
|
|
300
305
|
|
|
301
306
|
## API
|
|
302
307
|
|
|
303
|
-
-
|
|
304
|
-
-
|
|
305
|
-
-
|
|
306
|
-
-
|
|
307
|
-
-
|
|
308
|
-
-
|
|
309
|
-
-
|
|
310
|
-
-
|
|
311
|
-
-
|
|
312
|
-
-
|
|
313
|
-
-
|
|
314
|
-
-
|
|
315
|
-
-
|
|
316
|
-
-
|
|
317
|
-
-
|
|
318
|
-
-
|
|
319
|
-
-
|
|
308
|
+
- [`mqtt.connect()`](#connect)
|
|
309
|
+
- [`mqtt.Client()`](#client)
|
|
310
|
+
- [`mqtt.Client#connect()`](#client-connect)
|
|
311
|
+
- [`mqtt.Client#publish()`](#publish)
|
|
312
|
+
- [`mqtt.Client#subscribe()`](#subscribe)
|
|
313
|
+
- [`mqtt.Client#unsubscribe()`](#unsubscribe)
|
|
314
|
+
- [`mqtt.Client#end()`](#end)
|
|
315
|
+
- [`mqtt.Client#removeOutgoingMessage()`](#removeOutgoingMessage)
|
|
316
|
+
- [`mqtt.Client#reconnect()`](#reconnect)
|
|
317
|
+
- [`mqtt.Client#handleMessage()`](#handleMessage)
|
|
318
|
+
- [`mqtt.Client#connected`](#connected)
|
|
319
|
+
- [`mqtt.Client#reconnecting`](#reconnecting)
|
|
320
|
+
- [`mqtt.Client#getLastMessageId()`](#getLastMessageId)
|
|
321
|
+
- [`mqtt.Store()`](#store)
|
|
322
|
+
- [`mqtt.Store#put()`](#put)
|
|
323
|
+
- [`mqtt.Store#del()`](#del)
|
|
324
|
+
- [`mqtt.Store#createStream()`](#createStream)
|
|
325
|
+
- [`mqtt.Store#close()`](#close)
|
|
320
326
|
|
|
321
327
|
---
|
|
322
328
|
|
|
@@ -363,7 +369,7 @@ The arguments are:
|
|
|
363
369
|
the `connect` event. Typically a `net.Socket`.
|
|
364
370
|
- `options` is the client connection options (see: the [connect packet](https://github.com/mcollina/mqtt-packet#connect)). Defaults:
|
|
365
371
|
- `wsOptions`: is the WebSocket connection options. Default is `{}`.
|
|
366
|
-
It's specific for WebSockets. For possible options have a look at: https://github.com/websockets/ws/blob/master/doc/ws.md
|
|
372
|
+
It's specific for WebSockets. For possible options have a look at: <https://github.com/websockets/ws/blob/master/doc/ws.md>.
|
|
367
373
|
- `keepalive`: `60` seconds, set to `0` to disable
|
|
368
374
|
- `reschedulePings`: reschedule ping messages after sending packets (default `true`)
|
|
369
375
|
- `clientId`: `'mqttjs_' + Math.random().toString(16).substr(2, 8)`
|
|
@@ -381,9 +387,11 @@ The arguments are:
|
|
|
381
387
|
- `outgoingStore`: a [Store](#store) for the outgoing packets
|
|
382
388
|
- `queueQoSZero`: if connection is broken, queue outgoing QoS zero messages (default `true`)
|
|
383
389
|
- `customHandleAcks`: MQTT 5 feature of custom handling puback and pubrec packets. Its callback:
|
|
390
|
+
|
|
384
391
|
```js
|
|
385
392
|
customHandleAcks: function(topic, message, packet, done) {/*some logic wit colling done(error, reasonCode)*/}
|
|
386
393
|
```
|
|
394
|
+
|
|
387
395
|
- `autoUseTopicAlias`: enabling automatic Topic Alias using functionality
|
|
388
396
|
- `autoAssignTopicAlias`: enabling automatic Topic Alias assign functionality
|
|
389
397
|
- `properties`: properties MQTT 5.0.
|
|
@@ -418,6 +426,8 @@ The arguments are:
|
|
|
418
426
|
- `resubscribe` : if connection is broken and reconnects,
|
|
419
427
|
subscribed topics are automatically subscribed again (default `true`)
|
|
420
428
|
- `messageIdProvider`: custom messageId provider. when `new UniqueMessageIdProvider()` is set, then non conflict messageId is provided.
|
|
429
|
+
- `log`: custom log function. Default uses [debug](https://www.npmjs.com/package/debug) package.
|
|
430
|
+
- `manualConnect`: prevents the constructor to call `connect`. In this case after the `mqtt.connect` is called you should call `client.connect` manually.
|
|
421
431
|
|
|
422
432
|
In case mqtts (mqtt over tls) is required, the `options` object is
|
|
423
433
|
passed through to
|
|
@@ -491,7 +501,7 @@ The following TLS errors will be emitted as an `error` event:
|
|
|
491
501
|
|
|
492
502
|
`function () {}`
|
|
493
503
|
|
|
494
|
-
Emitted when
|
|
504
|
+
Emitted when [`mqtt.Client#end()`](#end) is called.
|
|
495
505
|
If a callback was passed to `mqtt.Client#end()`, this event is emitted once the
|
|
496
506
|
callback returns.
|
|
497
507
|
|
|
@@ -529,6 +539,12 @@ and connections
|
|
|
529
539
|
|
|
530
540
|
---
|
|
531
541
|
|
|
542
|
+
<a name="client-connect"></a>
|
|
543
|
+
|
|
544
|
+
### mqtt.Client#connect()
|
|
545
|
+
|
|
546
|
+
By default client connects when constructor is called. To prevent this you can set `manualConnect` option to `true` and call `client.connect()` manually.
|
|
547
|
+
|
|
532
548
|
<a name="publish"></a>
|
|
533
549
|
|
|
534
550
|
### mqtt.Client#publish(topic, message, [options], [callback])
|
|
@@ -740,9 +756,9 @@ Closes the Store.
|
|
|
740
756
|
|
|
741
757
|
### Via CDN
|
|
742
758
|
|
|
743
|
-
The MQTT.js bundle is available through http://unpkg.com
|
|
744
|
-
at https://unpkg.com/mqtt/dist/mqtt.min.js
|
|
745
|
-
See http://unpkg.com for the full documentation on version ranges.
|
|
759
|
+
The MQTT.js bundle is available through <http://unpkg.com>, specifically
|
|
760
|
+
at <https://unpkg.com/mqtt/dist/mqtt.min.js>.
|
|
761
|
+
See <http://unpkg.com> for the full documentation on version ranges.
|
|
746
762
|
|
|
747
763
|
<a name="browserify"></a>
|
|
748
764
|
|
package/bin/mqtt.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
'use strict'
|
|
3
2
|
|
|
4
3
|
/*
|
|
5
4
|
* Copyright (c) 2015-2015 MQTT.js contributors.
|
|
@@ -10,18 +9,19 @@
|
|
|
10
9
|
const path = require('path')
|
|
11
10
|
const commist = require('commist')()
|
|
12
11
|
const helpMe = require('help-me')({
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
dir: path.join(path.dirname(require.main.filename), '/../doc'),
|
|
13
|
+
ext: '.txt',
|
|
15
14
|
})
|
|
16
15
|
|
|
17
16
|
commist.register('publish', require('./pub'))
|
|
18
17
|
commist.register('subscribe', require('./sub'))
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
|
|
19
|
+
commist.register('version', () => {
|
|
20
|
+
console.log('MQTT.js version:', require('../package.json').version)
|
|
21
21
|
})
|
|
22
22
|
commist.register('help', helpMe.toStdout)
|
|
23
23
|
|
|
24
24
|
if (commist.parse(process.argv.slice(2)) !== null) {
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
console.log('No such command:', process.argv[2], '\n')
|
|
26
|
+
helpMe.toStdout()
|
|
27
27
|
}
|
package/bin/pub.js
CHANGED
|
@@ -1,146 +1,159 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const mqtt = require('../')
|
|
6
|
-
const pump = require('pump')
|
|
3
|
+
const mqtt = require('..')
|
|
4
|
+
const { pipeline, Writable } = require('readable-stream')
|
|
7
5
|
const path = require('path')
|
|
8
6
|
const fs = require('fs')
|
|
9
7
|
const concat = require('concat-stream')
|
|
10
|
-
const Writable = require('readable-stream').Writable
|
|
11
8
|
const helpMe = require('help-me')({
|
|
12
|
-
|
|
9
|
+
dir: path.join(__dirname, '..', 'doc'),
|
|
13
10
|
})
|
|
14
11
|
const minimist = require('minimist')
|
|
15
12
|
const split2 = require('split2')
|
|
16
13
|
|
|
17
|
-
function send
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
14
|
+
function send(args) {
|
|
15
|
+
const client = mqtt.connect(args)
|
|
16
|
+
client.on('connect', () => {
|
|
17
|
+
client.publish(args.topic, args.message, args, (err) => {
|
|
18
|
+
if (err) {
|
|
19
|
+
console.warn(err)
|
|
20
|
+
}
|
|
21
|
+
client.end()
|
|
22
|
+
})
|
|
23
|
+
})
|
|
24
|
+
client.on('error', (err) => {
|
|
25
|
+
console.warn(err)
|
|
26
|
+
client.end()
|
|
27
|
+
})
|
|
31
28
|
}
|
|
32
29
|
|
|
33
|
-
function multisend
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
30
|
+
function multisend(args) {
|
|
31
|
+
const client = mqtt.connect(args)
|
|
32
|
+
const sender = new Writable({
|
|
33
|
+
objectMode: true,
|
|
34
|
+
})
|
|
35
|
+
sender._write = (line, enc, cb) => {
|
|
36
|
+
client.publish(args.topic, line.trim(), args, cb)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
client.on('connect', () => {
|
|
40
|
+
pipeline(process.stdin, split2(), sender, (err) => {
|
|
41
|
+
client.end()
|
|
42
|
+
if (err) {
|
|
43
|
+
throw err
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
})
|
|
50
47
|
}
|
|
51
48
|
|
|
52
|
-
function start
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
49
|
+
function start(args) {
|
|
50
|
+
args = minimist(args, {
|
|
51
|
+
string: [
|
|
52
|
+
'hostname',
|
|
53
|
+
'username',
|
|
54
|
+
'password',
|
|
55
|
+
'key',
|
|
56
|
+
'cert',
|
|
57
|
+
'ca',
|
|
58
|
+
'message',
|
|
59
|
+
'clientId',
|
|
60
|
+
'i',
|
|
61
|
+
'id',
|
|
62
|
+
],
|
|
63
|
+
boolean: ['stdin', 'retain', 'help', 'insecure', 'multiline'],
|
|
64
|
+
alias: {
|
|
65
|
+
port: 'p',
|
|
66
|
+
hostname: ['h', 'host'],
|
|
67
|
+
topic: 't',
|
|
68
|
+
message: 'm',
|
|
69
|
+
qos: 'q',
|
|
70
|
+
clientId: ['i', 'id'],
|
|
71
|
+
retain: 'r',
|
|
72
|
+
username: 'u',
|
|
73
|
+
password: 'P',
|
|
74
|
+
stdin: 's',
|
|
75
|
+
multiline: 'M',
|
|
76
|
+
protocol: ['C', 'l'],
|
|
77
|
+
help: 'H',
|
|
78
|
+
ca: 'cafile',
|
|
79
|
+
},
|
|
80
|
+
default: {
|
|
81
|
+
host: 'localhost',
|
|
82
|
+
qos: 0,
|
|
83
|
+
retain: false,
|
|
84
|
+
topic: '',
|
|
85
|
+
message: '',
|
|
86
|
+
},
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
if (args.help) {
|
|
90
|
+
return helpMe.toStdout('publish')
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (args.key) {
|
|
94
|
+
args.key = fs.readFileSync(args.key)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (args.cert) {
|
|
98
|
+
args.cert = fs.readFileSync(args.cert)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (args.ca) {
|
|
102
|
+
args.ca = fs.readFileSync(args.ca)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (args.key && args.cert && !args.protocol) {
|
|
106
|
+
args.protocol = 'mqtts'
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (args.port) {
|
|
110
|
+
if (typeof args.port !== 'number') {
|
|
111
|
+
console.warn(
|
|
112
|
+
"# Port: number expected, '%s' was given.",
|
|
113
|
+
typeof args.port,
|
|
114
|
+
)
|
|
115
|
+
return
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (args['will-topic']) {
|
|
120
|
+
args.will = {}
|
|
121
|
+
args.will.topic = args['will-topic']
|
|
122
|
+
args.will.payload = args['will-message']
|
|
123
|
+
args.will.qos = args['will-qos']
|
|
124
|
+
args.will.retain = args['will-retain']
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (args.insecure) {
|
|
128
|
+
args.rejectUnauthorized = false
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
args.topic = (args.topic || args._.shift()).toString()
|
|
132
|
+
args.message = (args.message || args._.shift()).toString()
|
|
133
|
+
|
|
134
|
+
if (!args.topic) {
|
|
135
|
+
console.error('missing topic\n')
|
|
136
|
+
return helpMe.toStdout('publish')
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (args.stdin) {
|
|
140
|
+
if (args.multiline) {
|
|
141
|
+
multisend(args)
|
|
142
|
+
} else {
|
|
143
|
+
process.stdin.pipe(
|
|
144
|
+
concat((data) => {
|
|
145
|
+
args.message = data
|
|
146
|
+
send(args)
|
|
147
|
+
}),
|
|
148
|
+
)
|
|
149
|
+
}
|
|
150
|
+
} else {
|
|
151
|
+
send(args)
|
|
152
|
+
}
|
|
140
153
|
}
|
|
141
154
|
|
|
142
155
|
module.exports = start
|
|
143
156
|
|
|
144
157
|
if (require.main === module) {
|
|
145
|
-
|
|
158
|
+
start(process.argv.slice(2))
|
|
146
159
|
}
|