mqtt 5.16.0 → 5.17.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/README.md +75 -55
- package/build/bin/sub.js +1 -1
- package/build/bin/sub.js.map +1 -1
- package/build/lib/client.d.ts +6 -0
- package/build/lib/client.js +226 -8
- package/build/lib/client.js.map +1 -1
- package/build/lib/connect/index.js +50 -33
- package/build/lib/connect/index.js.map +1 -1
- package/build/lib/connect/tls.js +2 -4
- package/build/lib/connect/tls.js.map +1 -1
- package/build/lib/handlers/ack.js +9 -6
- package/build/lib/handlers/ack.js.map +1 -1
- package/build/lib/handlers/publish.js +1 -0
- package/build/lib/handlers/publish.js.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/dist/mqtt.esm.js +6 -6
- package/dist/mqtt.js +407 -340
- package/dist/mqtt.min.js +6 -6
- package/help/help.txt +2 -2
- package/help/publish.txt +6 -5
- package/help/subscribe.txt +6 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# 
|
|
2
2
|
|
|
3
|
-
](https://github.com/mqttjs/MQTT.js/actions/workflows/mqttjs-test.yml) [](https://codecov.io/gh/mqttjs/MQTT.js)
|
|
4
4
|
|
|
5
5
|
[](https://github.com/mqttjs/MQTT.js/graphs/commit-activity)
|
|
6
6
|
[](https://github.com/mqttjs/MQTT.js/pulls)
|
|
@@ -17,7 +17,7 @@ in JavaScript for node.js and the browser.
|
|
|
17
17
|
- [Installation](#install)
|
|
18
18
|
- [Example](#example)
|
|
19
19
|
- [React Native](#react-native)
|
|
20
|
-
- [Import Styles](#
|
|
20
|
+
- [Import Styles](#import_styles)
|
|
21
21
|
- [Command Line Tools](#cli)
|
|
22
22
|
- [API](#api)
|
|
23
23
|
- [Browser](#browser)
|
|
@@ -46,15 +46,13 @@ Guide](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.
|
|
|
46
46
|
**v4.0.0** (Released 04/2020) removes support for all end of life node versions, and now supports node v12 and v14. It also adds improvements to
|
|
47
47
|
debug logging, along with some feature additions.
|
|
48
48
|
|
|
49
|
-
As a **breaking change**, by default
|
|
49
|
+
As a **breaking change**, by default an error handler is built into the MQTT.js client, so if any
|
|
50
50
|
errors are emitted and the user has not created an event handler on the client for errors, the client will
|
|
51
51
|
not break as a result of unhandled errors. Additionally, typical TLS errors like `ECONNREFUSED`, `ECONNRESET` have been
|
|
52
52
|
added to a list of TLS errors that will be emitted from the MQTT.js client, and so can be handled as connection errors.
|
|
53
53
|
|
|
54
54
|
**v3.0.0** adds support for MQTT 5, support for node v10.x, and many fixes to improve reliability.
|
|
55
55
|
|
|
56
|
-
**Note:** MQTT v5 support is experimental as it has not been implemented by brokers yet.
|
|
57
|
-
|
|
58
56
|
**v2.0.0** removes support for node v0.8, v0.10 and v0.12, and it is 3x faster in sending
|
|
59
57
|
packets. It also removes all the deprecated functionality in v1.0.0,
|
|
60
58
|
mainly `mqtt.createConnection` and `mqtt.Server`. From v2.0.0,
|
|
@@ -198,12 +196,12 @@ MQTT.js uses the [debug](https://www.npmjs.com/package/debug#cmd) package for de
|
|
|
198
196
|
$env:DEBUG='mqttjs*'
|
|
199
197
|
```
|
|
200
198
|
|
|
201
|
-
<a name="
|
|
199
|
+
<a name="reconnection"></a>
|
|
202
200
|
|
|
203
201
|
## About Reconnection
|
|
204
202
|
|
|
205
|
-
An important part of any
|
|
206
|
-
drops off and the client needs to reconnect. MQTT has built-in reconnection
|
|
203
|
+
An important part of any MQTT connection is what to do when a connection
|
|
204
|
+
drops off and the client needs to reconnect. MQTT.js has built-in reconnection
|
|
207
205
|
support that can be configured to behave in ways that suit the application.
|
|
208
206
|
|
|
209
207
|
#### Refresh Authentication Options / Signed Urls with `transformWsUrl` (Websocket Only)
|
|
@@ -232,7 +230,7 @@ Example (update clientId & username on each reconnect):
|
|
|
232
230
|
|
|
233
231
|
const connection = await mqtt.connectAsync(<wss url>, {
|
|
234
232
|
...,
|
|
235
|
-
transformWsUrl:
|
|
233
|
+
transformWsUrl: transformWsUrl,
|
|
236
234
|
});
|
|
237
235
|
|
|
238
236
|
```
|
|
@@ -276,12 +274,20 @@ the final connection when it drops.
|
|
|
276
274
|
The default value is 1000 ms which means it will try to reconnect 1 second
|
|
277
275
|
after losing the connection.
|
|
278
276
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
277
|
+
The client reconnects whenever the network connection closes: when it fails to
|
|
278
|
+
open, when no CONNACK arrives within `connectTimeout`, or when it drops after a
|
|
279
|
+
successful connection. A connection the server actively refuses with a CONNACK
|
|
280
|
+
error is always closed, but by default it is _not_ retried.
|
|
281
|
+
|
|
282
|
+
To also retry after a CONNACK error, set `reconnectOnConnackError: true`.
|
|
282
283
|
|
|
283
|
-
|
|
284
|
-
`reconnectOnConnackError:
|
|
284
|
+
> **Changed in 5.16.0:** a refused CONNACK now always closes the connection.
|
|
285
|
+
> Before, with `reconnectOnConnackError: false` the socket was left open and
|
|
286
|
+
> the client usually retried anyway once the broker closed it. That accidental
|
|
287
|
+
> retry is gone, so if you relied on it (for example, an auth backend that is
|
|
288
|
+
> not ready at startup), set `reconnectOnConnackError: true`. See
|
|
289
|
+
> [GHSA-8phv-jwjm-93rr](https://github.com/mqttjs/MQTT.js/security/advisories/GHSA-8phv-jwjm-93rr)
|
|
290
|
+
> and [#2072](https://github.com/mqttjs/MQTT.js/issues/2072).
|
|
285
291
|
|
|
286
292
|
<a name="topicalias"></a>
|
|
287
293
|
|
|
@@ -297,13 +303,13 @@ example scenario:
|
|
|
297
303
|
1. PUBLISH topic:'t1', ta:1 (register)
|
|
298
304
|
2. PUBLISH topic:'t1' -> topic:'', ta:1 (auto use existing map entry)
|
|
299
305
|
3. PUBLISH topic:'t2', ta:1 (register overwrite)
|
|
300
|
-
4. PUBLISH topic:'t2' -> topic:'', ta:1 (auto use existing map entry based on the
|
|
306
|
+
4. PUBLISH topic:'t2' -> topic:'', ta:1 (auto use existing map entry based on the recent map)
|
|
301
307
|
5. PUBLISH topic:'t1' (t1 is no longer mapped to ta:1)
|
|
302
308
|
```
|
|
303
309
|
|
|
304
310
|
User doesn't need to manage which topic is mapped to which topic alias.
|
|
305
|
-
If the user
|
|
306
|
-
If the user
|
|
311
|
+
If the user wants to register topic alias, then publish topic with topic alias.
|
|
312
|
+
If the user wants to use topic alias, then publish topic without topic alias. If there is a mapped topic alias then add it as a property and update the topic to empty string.
|
|
307
313
|
|
|
308
314
|
### Enabling automatic Topic Alias assign
|
|
309
315
|
|
|
@@ -316,8 +322,8 @@ example scenario:
|
|
|
316
322
|
The broker returns CONNACK (TopicAliasMaximum:3)
|
|
317
323
|
1. PUBLISH topic:'t1' -> 't1', ta:1 (auto assign t1:1 and register)
|
|
318
324
|
2. PUBLISH topic:'t1' -> '' , ta:1 (auto use existing map entry)
|
|
319
|
-
3. PUBLISH topic:'t2' -> 't2', ta:2 (auto assign
|
|
320
|
-
4. PUBLISH topic:'t3' -> 't3', ta:3 (auto assign
|
|
325
|
+
3. PUBLISH topic:'t2' -> 't2', ta:2 (auto assign t2:2 and register. 2 was vacant)
|
|
326
|
+
4. PUBLISH topic:'t3' -> 't3', ta:3 (auto assign t3:3 and register. 3 was vacant)
|
|
321
327
|
5. PUBLISH topic:'t4' -> 't4', ta:1 (LRU entry is overwritten)
|
|
322
328
|
```
|
|
323
329
|
|
|
@@ -345,7 +351,7 @@ Also user can manually register topic-alias pair using PUBLISH topic:'some', ta:
|
|
|
345
351
|
- [`mqtt.Client#connected`](#connected)
|
|
346
352
|
- [`mqtt.Client#keepalive`](#keepalive)
|
|
347
353
|
- [`mqtt.Client#serverProperties`](#serverProperties)
|
|
348
|
-
- [`mqtt.Client#reconnecting`](#reconnecting)
|
|
354
|
+
- [`mqtt.Client#reconnecting`](#client-reconnecting)
|
|
349
355
|
- [`mqtt.Client#getLastMessageId()`](#getLastMessageId)
|
|
350
356
|
- [`mqtt.Store()`](#store)
|
|
351
357
|
- [`mqtt.Store#put()`](#put)
|
|
@@ -363,12 +369,19 @@ Connects to the broker specified by the given url and options and
|
|
|
363
369
|
returns a [Client](#client).
|
|
364
370
|
|
|
365
371
|
The URL can be on the following protocols: 'mqtt', 'mqtts', 'tcp',
|
|
366
|
-
'tls', 'ws', 'wss', 'wxs', 'alis'. If you are trying to connect to a unix socket just append the `+unix` suffix to the protocol (ex: `mqtt+unix`). This will set the `unixSocket` property automatically.
|
|
372
|
+
'tls', 'ssl', 'ws', 'wss', 'wx', 'wxs', 'ali', 'alis'. If you are trying to connect to a unix socket just append the `+unix` suffix to the protocol (ex: `mqtt+unix`). This will set the `unixSocket` property automatically.
|
|
373
|
+
|
|
374
|
+
A secure protocol (`mqtts`, `tls`, `ssl`, `wss`, `wxs`, `alis`) the current
|
|
375
|
+
environment has no transport for is only ever replaced by a secure one:
|
|
376
|
+
`mqtts`/`tls`/`ssl` become `wss` in the browser (or with
|
|
377
|
+
`forceNativeWebSocket`), and `wxs`/`alis` become `mqtts` on Node.js. If no
|
|
378
|
+
secure transport is available, `connect()` throws instead of connecting without
|
|
379
|
+
TLS.
|
|
367
380
|
|
|
368
|
-
The
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
381
|
+
The first argument can also be an options object instead of a URL string, e.g.
|
|
382
|
+
`mqtt.connect({ host, port, protocol: 'mqtts' })`. It is then used as the
|
|
383
|
+
options as-is, so don't pass a second argument: when both are given, the object
|
|
384
|
+
is ignored.
|
|
372
385
|
|
|
373
386
|
You can also specify a `servers` options with content: `[{ host:
|
|
374
387
|
'localhost', port: 1883 }, ... ]`, in that case that array is iterated
|
|
@@ -399,8 +412,8 @@ events.
|
|
|
399
412
|
|
|
400
413
|
The `Client` class wraps a client connection to an
|
|
401
414
|
MQTT broker over an arbitrary transport method (TCP, TLS,
|
|
402
|
-
WebSocket,
|
|
403
|
-
`Client` is an [EventEmitter](https://nodejs.org/en/learn/asynchronous-work/the-nodejs-event-emitter) that has
|
|
415
|
+
WebSocket, etc.).
|
|
416
|
+
`Client` is an [EventEmitter](https://nodejs.org/en/learn/asynchronous-work/the-nodejs-event-emitter) that has its own [events](#events)
|
|
404
417
|
|
|
405
418
|
`Client` automatically handles the following:
|
|
406
419
|
|
|
@@ -426,7 +439,8 @@ The arguments are:
|
|
|
426
439
|
- `reconnectPeriod`: `1000` milliseconds, interval between two
|
|
427
440
|
reconnections. Disable auto reconnect by setting to `0`.
|
|
428
441
|
- `reconnectOnConnackError`: `false`, whether to also reconnect if a CONNACK
|
|
429
|
-
is received with an error.
|
|
442
|
+
is received with an error. Since 5.16.0 a refused CONNACK always closes the
|
|
443
|
+
connection, and without this option no retry follows.
|
|
430
444
|
- `connectTimeout`: `30 * 1000` milliseconds, time to wait before a
|
|
431
445
|
CONNACK is received
|
|
432
446
|
- `username`: the username required by your broker, if any
|
|
@@ -456,7 +470,7 @@ The arguments are:
|
|
|
456
470
|
- `authenticationMethod`: the name of the authentication method used for extended authentication `string`,
|
|
457
471
|
- `authenticationData`: Binary Data containing authentication data `binary`
|
|
458
472
|
- `authPacket`: settings for auth packet `object`
|
|
459
|
-
- `will`: a message that will sent by the broker automatically when
|
|
473
|
+
- `will`: a message that will be sent by the broker automatically when
|
|
460
474
|
the client disconnect badly. The format is:
|
|
461
475
|
- `topic`: the topic to publish
|
|
462
476
|
- `payload`: the message to publish
|
|
@@ -473,7 +487,7 @@ The arguments are:
|
|
|
473
487
|
- `transformWsUrl` : optional `(url, options, client) => url` function
|
|
474
488
|
For ws/wss protocols only. Can be used to implement signing
|
|
475
489
|
urls which upon reconnect can have become expired.
|
|
476
|
-
- `createWebsocket` : optional `url, websocketSubProtocols, options) => Websocket` function
|
|
490
|
+
- `createWebsocket` : optional `(url, websocketSubProtocols, options) => Websocket` function
|
|
477
491
|
For ws/wss protocols only. Can be used to implement a custom websocket subprotocol or implementation.
|
|
478
492
|
- `resubscribe` : if connection is broken and reconnects,
|
|
479
493
|
subscribed topics are automatically subscribed again (default `true`)
|
|
@@ -482,17 +496,17 @@ The arguments are:
|
|
|
482
496
|
- `messageIdProvider`: custom messageId provider. when `new UniqueMessageIdProvider()` is set, then non conflict messageId is provided.
|
|
483
497
|
- `log`: custom log function. Default uses [debug](https://www.npmjs.com/package/debug) package.
|
|
484
498
|
- `manualConnect`: prevents the constructor to call `connect`. In this case after the `mqtt.connect` is called you should call `client.connect` manually.
|
|
485
|
-
- `timerVariant`: defaults to `auto`, which tries to determine which timer is most appropriate for
|
|
499
|
+
- `timerVariant`: defaults to `auto`, which tries to determine which timer is most appropriate for your environment, if you're having detection issues, you can set it to `worker` or `native`. If none suits you, you can pass a timer object with set and clear properties:
|
|
486
500
|
```js
|
|
487
501
|
timerVariant: {
|
|
488
502
|
set: (func, timer) => setInterval(func, timer),
|
|
489
503
|
clear: (id) => clearInterval(id)
|
|
490
504
|
}
|
|
491
505
|
```
|
|
492
|
-
- `forceNativeWebSocket`: set to true if you're having detection issues (i.e. the `ws does not work in the browser` exception) to force the use of native WebSocket. It
|
|
506
|
+
- `forceNativeWebSocket`: set to true if you're having detection issues (i.e. the `ws does not work in the browser` exception) to force the use of native WebSocket. It only applies to the client that sets it; other clients keep using the detection result.
|
|
493
507
|
- `unixSocket`: if you want to connect to a unix socket, set this to true
|
|
494
508
|
|
|
495
|
-
Instead of setting `socksProxy` you can also
|
|
509
|
+
Instead of setting `socksProxy` you can also supply the same parameter via the environment variable `MQTTJS_SOCKS_PROXY`.
|
|
496
510
|
|
|
497
511
|
In case mqtts (mqtt over tls) is required, the `options` object is passed through to [`tls.connect()`](http://nodejs.org/api/tls.html#tls_tls_connect_options_callback). If using a **self-signed certificate**, set `rejectUnauthorized: false`. However, be cautious as this exposes you to potential man in the middle attacks and isn't recommended for production.
|
|
498
512
|
|
|
@@ -554,7 +568,8 @@ Emitted when the client goes offline.
|
|
|
554
568
|
Emitted when the client cannot connect (i.e. connack rc != 0) or when a
|
|
555
569
|
parsing error occurs.
|
|
556
570
|
|
|
557
|
-
|
|
571
|
+
Any stream error that has an error `code` (Node.js only) is emitted as an
|
|
572
|
+
`error` event, for example:
|
|
558
573
|
|
|
559
574
|
- `ECONNREFUSED`
|
|
560
575
|
- `ECONNRESET`
|
|
@@ -565,9 +580,9 @@ The following TLS errors will be emitted as an `error` event:
|
|
|
565
580
|
|
|
566
581
|
`function () {}`
|
|
567
582
|
|
|
568
|
-
Emitted when [`mqtt.Client#end()`](#end)
|
|
569
|
-
If a callback was passed to `mqtt.Client#end()`,
|
|
570
|
-
|
|
583
|
+
Emitted when [`mqtt.Client#end()`](#end) completes, once the stores are
|
|
584
|
+
closed. If a callback was passed to `mqtt.Client#end()`, it is called right
|
|
585
|
+
after this event.
|
|
571
586
|
|
|
572
587
|
#### Event `'message'`
|
|
573
588
|
|
|
@@ -641,6 +656,7 @@ Publish a message to a topic
|
|
|
641
656
|
Async [`publish`](#publish). Returns a `Promise<Packet | undefined>`.
|
|
642
657
|
|
|
643
658
|
A packet is anything that has a `messageId` property.
|
|
659
|
+
|
|
644
660
|
---
|
|
645
661
|
|
|
646
662
|
<a name="subscribe"></a>
|
|
@@ -738,9 +754,12 @@ After this function is called, the messageId is released and becomes reusable.
|
|
|
738
754
|
|
|
739
755
|
<a name="reconnect"></a>
|
|
740
756
|
|
|
741
|
-
### mqtt.Client#reconnect()
|
|
757
|
+
### mqtt.Client#reconnect([options])
|
|
742
758
|
|
|
743
|
-
Connect again using the same options as connect()
|
|
759
|
+
Connect again using the same options as connect(). `options` can hold
|
|
760
|
+
`incomingStore` and `outgoingStore` to use for the new connection. Without
|
|
761
|
+
`options`, new in-memory stores replace the ones in use, including stores
|
|
762
|
+
passed to `mqtt.connect()`.
|
|
744
763
|
|
|
745
764
|
---
|
|
746
765
|
|
|
@@ -778,7 +797,7 @@ Three cases make the broker value irrelevant:
|
|
|
778
797
|
configured `keepalive` stays in use.
|
|
779
798
|
- a refused CONNACK opens no connection to keep alive, so its `Server Keep Alive`
|
|
780
799
|
is ignored here. [`client.serverProperties`](#serverProperties) still reports
|
|
781
|
-
it.
|
|
800
|
+
it until the `close` that follows the refusal.
|
|
782
801
|
|
|
783
802
|
---
|
|
784
803
|
|
|
@@ -791,15 +810,15 @@ connection, `undefined` when there is none. Read only, and reset both when the
|
|
|
791
810
|
connection closes and when the next connection attempt starts.
|
|
792
811
|
|
|
793
812
|
A refused CONNACK counts, which is the only place `reasonString` and
|
|
794
|
-
`serverReference` ever appear.
|
|
795
|
-
|
|
796
|
-
|
|
813
|
+
`serverReference` ever appear. Read them from the `error` handler: a refusal
|
|
814
|
+
always closes the connection, and they are cleared when the `close` that follows
|
|
815
|
+
it is emitted. They describe a connection that was never opened, which is why
|
|
797
816
|
[`client.keepalive`](#keepalive) ignores their `serverKeepAlive`.
|
|
798
817
|
|
|
799
818
|
Broker values are kept here and are never written back into the options object
|
|
800
819
|
given to `mqtt.connect()`.
|
|
801
820
|
|
|
802
|
-
#### Migrating from 5.
|
|
821
|
+
#### Migrating from 5.15 and earlier
|
|
803
822
|
|
|
804
823
|
The broker no longer overwrites the options object:
|
|
805
824
|
|
|
@@ -817,18 +836,18 @@ Two things changed in how an inbound packet larger than
|
|
|
817
836
|
|
|
818
837
|
- the limit now counts the whole packet. The fixed header byte and the bytes
|
|
819
838
|
encoding the Remaining Length count toward it, as MQTT 5.0 requires
|
|
820
|
-
(§3.1.2.11.4 with §2.1.4); 5.
|
|
839
|
+
(§3.1.2.11.4 with §2.1.4); 5.15 and earlier compared the limit against the Remaining
|
|
821
840
|
Length alone. The same configured limit therefore rejects packets 2 to 5 bytes
|
|
822
841
|
smaller than it used to - 2 bytes for packets under 128 bytes, one more for
|
|
823
842
|
every further step of the Remaining Length. A limit set right at the size of
|
|
824
843
|
the largest packet expected needs raising by that much.
|
|
825
|
-
- an oversized packet no longer ends the client. 5.
|
|
844
|
+
- an oversized packet no longer ends the client. 5.15 and earlier called
|
|
826
845
|
`end({ reasonCode: 149 })`: it sent a DISCONNECT and stopped the client for
|
|
827
846
|
good. The connection is now dropped like any other failure, no DISCONNECT is
|
|
828
847
|
sent, and the client reconnects. This was deliberate - one oversized packet
|
|
829
848
|
was a kill switch any broker could pull permanently - but it cuts both ways: a
|
|
830
849
|
broker holding a retained message that is always too large becomes a reconnect
|
|
831
|
-
loop where 5.
|
|
850
|
+
loop where 5.15 and earlier stopped after the first one. Applications that want the old
|
|
832
851
|
stop can call `end()` from the `error` handler on reason code 149.
|
|
833
852
|
|
|
834
853
|
---
|
|
@@ -841,7 +860,7 @@ Number : get last message id. This is for sent messages only.
|
|
|
841
860
|
|
|
842
861
|
---
|
|
843
862
|
|
|
844
|
-
<a name="reconnecting"></a>
|
|
863
|
+
<a name="client-reconnecting"></a>
|
|
845
864
|
|
|
846
865
|
### mqtt.Client#reconnecting
|
|
847
866
|
|
|
@@ -919,8 +938,8 @@ Closes the Store.
|
|
|
919
938
|
|
|
920
939
|
While the [ws](https://www.npmjs.com/package/ws) module is used in NodeJS, [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) is used in browsers. This is totally transparent to users except for the following:
|
|
921
940
|
|
|
922
|
-
- The `
|
|
923
|
-
- Browsers
|
|
941
|
+
- The `wsOptions` option is not supported in browsers.
|
|
942
|
+
- Browsers don't allow catching many WebSocket errors for [security reasons](https://stackoverflow.com/a/31003057) as:
|
|
924
943
|
|
|
925
944
|
> Access to this information could allow a malicious Web page to gain information about your network, so they require browsers report all connection-time errors in an indistinguishable way.
|
|
926
945
|
|
|
@@ -944,14 +963,16 @@ import mqtt from 'mqtt'
|
|
|
944
963
|
|
|
945
964
|
This will be automatically handled by your bundler.
|
|
946
965
|
|
|
947
|
-
Otherwise you can
|
|
966
|
+
Otherwise you can import the ESM bundle directly:
|
|
948
967
|
|
|
949
968
|
```js
|
|
950
|
-
import * as mqtt from 'mqtt/dist/mqtt'
|
|
951
|
-
import * as mqtt from 'mqtt/dist/mqtt.min'
|
|
952
969
|
import mqtt from 'mqtt/dist/mqtt.esm'
|
|
953
970
|
```
|
|
954
971
|
|
|
972
|
+
The `iife` bundles (`mqtt.js` and `mqtt.min.js`) have no exports, so they
|
|
973
|
+
can't be imported. Load them with a `<script>` tag instead: they define a
|
|
974
|
+
global `mqtt` (see [Via CDN](#cdn)).
|
|
975
|
+
|
|
955
976
|
<a name="cdn"></a>
|
|
956
977
|
|
|
957
978
|
### Via CDN
|
|
@@ -968,7 +989,7 @@ Here is how QoS works:
|
|
|
968
989
|
|
|
969
990
|
- QoS 0 : received **at most once** : The packet is sent, and that's it. There is no validation about whether it has been received.
|
|
970
991
|
- QoS 1 : received **at least once** : The packet is sent and stored as long as the client has not received a confirmation from the server. MQTT ensures that it _will_ be received, but there can be duplicates.
|
|
971
|
-
- QoS 2 : received **exactly once** : Same as QoS 1 but there
|
|
992
|
+
- QoS 2 : received **exactly once** : Same as QoS 1 but there are no duplicates.
|
|
972
993
|
|
|
973
994
|
About data consumption, obviously, QoS 2 > QoS 1 > QoS 0, if that's a concern to you.
|
|
974
995
|
|
|
@@ -1005,7 +1026,6 @@ const client = mqtt.connect("wxs://test.mosquitto.org", {
|
|
|
1005
1026
|
### Ali Mini Program
|
|
1006
1027
|
|
|
1007
1028
|
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).
|
|
1008
|
-
<a name="example"></a>
|
|
1009
1029
|
|
|
1010
1030
|
```js
|
|
1011
1031
|
const mqtt = require("mqtt");
|
package/build/bin/sub.js
CHANGED
package/build/bin/sub.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sub.js","sourceRoot":"","sources":["../../src/bin/sub.ts"],"names":[],"mappings":";;;;;;AAaA,wBAgIC;AA3ID,gDAAuB;AACvB,4CAAmB;AACnB,wDAA+B;AAC/B,sDAA0B;AAE1B,kCAAiC;AAEjC,MAAM,MAAM,GAAG,IAAA,iBAAI,EAAC;IACnB,GAAG,EAAE,cAAI,CAAC,IAAI,CAAC,SAAS,EAAE,QAAQ,EAAE,MAAM,CAAC;CAC3C,CAAC,CAA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.d.ts
CHANGED
|
@@ -192,6 +192,7 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
|
|
|
192
192
|
private _releaseIncomingQoS2Publish;
|
|
193
193
|
private _discardIncomingQoS2State;
|
|
194
194
|
connect(): this;
|
|
195
|
+
private _onParserError;
|
|
195
196
|
private _onConnackTimeout;
|
|
196
197
|
publish(topic: string, message: string | Buffer): MqttClient;
|
|
197
198
|
publish(topic: string, message: string | Buffer, callback?: PacketCallback): MqttClient;
|
|
@@ -231,6 +232,9 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
|
|
|
231
232
|
private _cleanUp;
|
|
232
233
|
private _storeAndSend;
|
|
233
234
|
private _applyTopicAlias;
|
|
235
|
+
private _checkOutgoingPublish;
|
|
236
|
+
private _publishAsSent;
|
|
237
|
+
private _failPublish;
|
|
234
238
|
private _noop;
|
|
235
239
|
private _writePacket;
|
|
236
240
|
private _sendPacket;
|
|
@@ -248,5 +252,7 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
|
|
|
248
252
|
private _invokeStoreProcessingQueue;
|
|
249
253
|
private _invokeAllStoreProcessingQueue;
|
|
250
254
|
private _flushStoreProcessingQueue;
|
|
255
|
+
private _sendNeverSentPacket;
|
|
256
|
+
private _discardOutgoingSessionPacket;
|
|
251
257
|
private _removeOutgoingAndStoreMessage;
|
|
252
258
|
}
|