mqtt 5.0.0-beta.2 → 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 CHANGED
@@ -2,10 +2,15 @@
2
2
 
3
3
  ![Github Test Status](https://github.com/mqttjs/MQTT.js/workflows/MQTT.js%20CI/badge.svg) [![codecov](https://codecov.io/gh/mqttjs/MQTT.js/branch/master/graph/badge.svg)](https://codecov.io/gh/mqttjs/MQTT.js)
4
4
 
5
+ [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) [![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/mqttjs/MQTT.js/graphs/commit-activity)
6
+ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/mqttjs/MQTT.js/pulls)\
7
+
8
+ ![NPM Version](https://img.shields.io/npm/v/mqtt?logo=npm) ![NPM Downloads](https://img.shields.io/npm/dm/mqtt.svg)
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-beta.0](https://github.com/mqttjs/MQTT.js/releases/tag/v5.0.0-beta.0) is now available! Try it out and give us feedback! `npm i mqtt@5.0.0-beta.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", function () {
100
- client.subscribe("presence", function (err) {
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", function (topic, message) {
112
+ client.on("message", (topic, message) => {
108
113
  // message is Buffer
109
114
  console.log(message.toString());
110
115
  client.end();
@@ -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
- - <a href="#connect"><code>mqtt.<b>connect()</b></code></a>
304
- - <a href="#client"><code>mqtt.<b>Client()</b></code></a>
305
- - <a href="#publish"><code>mqtt.Client#<b>publish()</b></code></a>
306
- - <a href="#subscribe"><code>mqtt.Client#<b>subscribe()</b></code></a>
307
- - <a href="#unsubscribe"><code>mqtt.Client#<b>unsubscribe()</b></code></a>
308
- - <a href="#end"><code>mqtt.Client#<b>end()</b></code></a>
309
- - <a href="#removeOutgoingMessage"><code>mqtt.Client#<b>removeOutgoingMessage()</b></code></a>
310
- - <a href="#reconnect"><code>mqtt.Client#<b>reconnect()</b></code></a>
311
- - <a href="#handleMessage"><code>mqtt.Client#<b>handleMessage()</b></code></a>
312
- - <a href="#connected"><code>mqtt.Client#<b>connected</b></code></a>
313
- - <a href="#reconnecting"><code>mqtt.Client#<b>reconnecting</b></code></a>
314
- - <a href="#getLastMessageId"><code>mqtt.Client#<b>getLastMessageId()</b></code></a>
315
- - <a href="#store"><code>mqtt.<b>Store()</b></code></a>
316
- - <a href="#put"><code>mqtt.Store#<b>put()</b></code></a>
317
- - <a href="#del"><code>mqtt.Store#<b>del()</b></code></a>
318
- - <a href="#createStream"><code>mqtt.Store#<b>createStream()</b></code></a>
319
- - <a href="#close"><code>mqtt.Store#<b>close()</b></code></a>
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 <a href="#end"><code>mqtt.Client#<b>end()</b></code></a> is called.
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, specifically
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
- dir: path.join(path.dirname(require.main.filename), '/../doc'),
14
- ext: '.txt'
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
- commist.register('version', function () {
20
- console.log('MQTT.js version:', require('./../package.json').version)
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
- console.log('No such command:', process.argv[2], '\n')
26
- helpMe.toStdout()
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
- 'use strict'
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
- dir: path.join(__dirname, '..', 'doc')
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 (args) {
18
- const client = mqtt.connect(args)
19
- client.on('connect', function () {
20
- client.publish(args.topic, args.message, args, function (err) {
21
- if (err) {
22
- console.warn(err)
23
- }
24
- client.end()
25
- })
26
- })
27
- client.on('error', function (err) {
28
- console.warn(err)
29
- client.end()
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 (args) {
34
- const client = mqtt.connect(args)
35
- const sender = new Writable({
36
- objectMode: true
37
- })
38
- sender._write = function (line, enc, cb) {
39
- client.publish(args.topic, line.trim(), args, cb)
40
- }
41
-
42
- client.on('connect', function () {
43
- pump(process.stdin, split2(), sender, function (err) {
44
- client.end()
45
- if (err) {
46
- throw err
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 (args) {
53
- args = minimist(args, {
54
- string: ['hostname', 'username', 'password', 'key', 'cert', 'ca', 'message', 'clientId', 'i', 'id'],
55
- boolean: ['stdin', 'retain', 'help', 'insecure', 'multiline'],
56
- alias: {
57
- port: 'p',
58
- hostname: ['h', 'host'],
59
- topic: 't',
60
- message: 'm',
61
- qos: 'q',
62
- clientId: ['i', 'id'],
63
- retain: 'r',
64
- username: 'u',
65
- password: 'P',
66
- stdin: 's',
67
- multiline: 'M',
68
- protocol: ['C', 'l'],
69
- help: 'H',
70
- ca: 'cafile'
71
- },
72
- default: {
73
- host: 'localhost',
74
- qos: 0,
75
- retain: false,
76
- topic: '',
77
- message: ''
78
- }
79
- })
80
-
81
- if (args.help) {
82
- return helpMe.toStdout('publish')
83
- }
84
-
85
- if (args.key) {
86
- args.key = fs.readFileSync(args.key)
87
- }
88
-
89
- if (args.cert) {
90
- args.cert = fs.readFileSync(args.cert)
91
- }
92
-
93
- if (args.ca) {
94
- args.ca = fs.readFileSync(args.ca)
95
- }
96
-
97
- if (args.key && args.cert && !args.protocol) {
98
- args.protocol = 'mqtts'
99
- }
100
-
101
- if (args.port) {
102
- if (typeof args.port !== 'number') {
103
- console.warn('# Port: number expected, \'%s\' was given.', typeof args.port)
104
- return
105
- }
106
- }
107
-
108
- if (args['will-topic']) {
109
- args.will = {}
110
- args.will.topic = args['will-topic']
111
- args.will.payload = args['will-message']
112
- args.will.qos = args['will-qos']
113
- args.will.retain = args['will-retain']
114
- }
115
-
116
- if (args.insecure) {
117
- args.rejectUnauthorized = false
118
- }
119
-
120
- args.topic = (args.topic || args._.shift()).toString()
121
- args.message = (args.message || args._.shift()).toString()
122
-
123
- if (!args.topic) {
124
- console.error('missing topic\n')
125
- return helpMe.toStdout('publish')
126
- }
127
-
128
- if (args.stdin) {
129
- if (args.multiline) {
130
- multisend(args)
131
- } else {
132
- process.stdin.pipe(concat(function (data) {
133
- args.message = data
134
- send(args)
135
- }))
136
- }
137
- } else {
138
- send(args)
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
- start(process.argv.slice(2))
158
+ start(process.argv.slice(2))
146
159
  }