Skip to content

Commit

Permalink
fixup! fixup! feat: add forceNativeWebSocket client option
Browse files Browse the repository at this point in the history
chore: remove test_store folder pushed
refactor: load protocols only once
refactor: use forceNativeWebSocket only for ws choice
doc(README): typo + update forceNativeWebSocket behaviour description
  • Loading branch information
EmixamPP committed Jul 26, 2024
1 parent 94b8323 commit d1adfc2
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 30 deletions.
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ The arguments are:
- `messageExpiryInterval`: value is the lifetime of the Will Message in seconds and is sent as the Publication Expiry Interval when the Server publishes the Will Message `number`,
- `contentType`: describing the content of the Will Message `string`,
- `responseTopic`: String which is used as the Topic Name for a response message `string`,
- `correlationData`: The Correlation Data is used b`y the sender of the Request Message to identify which request the Response Message is for when it is received `binary`,
- `correlationData`: The Correlation Data is used by the sender of the Request Message to identify which request the Response Message is for when it is received `binary`,
- `userProperties`: The User Property is allowed to appear multiple times to represent multiple name, value pairs `object`
- `transformWsUrl` : optional `(url, options, client) => url` function
For ws/wss protocols only. Can be used to implement signing
Expand All @@ -467,14 +467,13 @@ The arguments are:
- `log`: custom log function. Default uses [debug](https://www.npmjs.com/package/debug) package.
- `manualConnect`: prevents the constructor to call `connect`. In this case after the `mqtt.connect` is called you should call `client.connect` manually.
- `timerVariant`: defaults to `auto`, which tries to determine which timer is most appropriate for you 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:
```js
timerVariant: {
set: (func, timer) => setInterval(func, timer),
clear: (id) => clearInterval(id)
}
```

- `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.
```js
timerVariant: {
set: (func, timer) => setInterval(func, timer),
clear: (id) => clearInterval(id)
}
```
- `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 is important to note that if set to true for the first client created, then all the clients will use native WebSocket. And conversely, if not set or set to false, all will use the detection result.
- `unixSocket`: if you want to connect to a unix socket, set this to true
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.
Expand Down
2 changes: 1 addition & 1 deletion src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
} else {
this.log(
'MqttClient :: environment',
options.forceNativeWebSocket || isBrowser ? 'browser' : 'node',
isBrowser ? 'browser' : 'node',
)
}

Expand Down
40 changes: 22 additions & 18 deletions src/lib/connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ if (typeof process?.nextTick !== 'function') {

const debug = _debug('mqttjs')

let protocols: Record<string, StreamBuilder> = null

/**
* Parse the auth attribute and merge username and password in the options object.
*
Expand Down Expand Up @@ -133,26 +135,28 @@ function connect(
}
}

const protocols: Record<string, StreamBuilder> = {}

if (!opts.forceNativeWebSocket && !isBrowser) {
protocols.ws = require('./ws').streamBuilder
protocols.wss = require('./ws').streamBuilder

protocols.mqtt = require('./tcp').default
protocols.tcp = require('./tcp').default
protocols.ssl = require('./tls').default
protocols.tls = protocols.ssl
protocols.mqtts = require('./tls').default
} else {
protocols.ws = require('./ws').browserStreamBuilder
protocols.wss = require('./ws').browserStreamBuilder
// only loads the protocols once
if (!protocols) {
protocols = {}
if (!isBrowser && !opts.forceNativeWebSocket) {
protocols.ws = require('./ws').streamBuilder
protocols.wss = require('./ws').streamBuilder

protocols.mqtt = require('./tcp').default
protocols.tcp = require('./tcp').default
protocols.ssl = require('./tls').default
protocols.tls = protocols.ssl
protocols.mqtts = require('./tls').default
} else {
protocols.ws = require('./ws').browserStreamBuilder
protocols.wss = require('./ws').browserStreamBuilder

protocols.wx = require('./wx').default
protocols.wxs = require('./wx').default
protocols.wx = require('./wx').default
protocols.wxs = require('./wx').default

protocols.ali = require('./ali').default
protocols.alis = require('./ali').default
protocols.ali = require('./ali').default
protocols.alis = require('./ali').default
}
}

if (!protocols[opts.protocol]) {
Expand Down
Binary file removed test-store_ApvD3c/000003.log
Binary file not shown.
1 change: 0 additions & 1 deletion test-store_ApvD3c/CURRENT

This file was deleted.

Empty file removed test-store_ApvD3c/LOCK
Empty file.
1 change: 0 additions & 1 deletion test-store_ApvD3c/LOG

This file was deleted.

Binary file removed test-store_ApvD3c/MANIFEST-000002
Binary file not shown.

0 comments on commit d1adfc2

Please sign in to comment.