Skip to content

Commit

Permalink
feat: add forceNativeWebSocket client option
Browse files Browse the repository at this point in the history
  • Loading branch information
EmixamPP committed Jul 25, 2024
1 parent 4960dab commit 6837093
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
3 changes: 2 additions & 1 deletion 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 by 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 b`y 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 @@ -474,6 +474,7 @@ The arguments are:
}
```

- `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.
- `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
6 changes: 5 additions & 1 deletion src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ export interface IClientOptions extends ISecureClientOptions {
* or pass a custom timer object
*/
timerVariant?: TimerVariant | Timer
/**
* false, set to true to force the use of native WebSocket if you're having issues with the detection
*/
forceNativeWebSocket?: boolean
}

export interface IClientPublishOptions {
Expand Down Expand Up @@ -512,7 +516,7 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
} else {
this.log(
'MqttClient :: environment',
isBrowser ? 'browser' : 'node',
(options.forceNativeWebSocket || isBrowser) ? 'browser' : 'node',
)
}

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

const debug = _debug('mqttjs')

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

if (!IS_BROWSER) {
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.wx = require('./wx').default
protocols.wxs = require('./wx').default

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

protocols.ws = require('./ws').default
protocols.wss = require('./ws').default

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

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

if (!opts.forceNativeWebSocket && !IS_BROWSER) {
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.ali = require('./ali').default
protocols.alis = require('./ali').default
}

if (!protocols[opts.protocol]) {
const isSecure = ['mqtts', 'wss'].indexOf(opts.protocol) !== -1
// returns the first available protocol based on available protocols (that depends on environment)
Expand Down
4 changes: 2 additions & 2 deletions src/lib/connect/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function setDefaultOpts(opts: IClientOptions) {
if (!opts.wsOptions) {
options.wsOptions = {}
}
if (!IS_BROWSER && opts.protocol === 'wss') {
if (!IS_BROWSER && !opts.forceNativeWebSocket && opts.protocol === 'wss') {
// Add cert/key/ca etc options
WSS_OPTIONS.forEach((prop) => {
if (
Expand Down Expand Up @@ -298,4 +298,4 @@ const browserStreamBuilder: StreamBuilder = (client, opts) => {
return stream
}

export default IS_BROWSER ? browserStreamBuilder : streamBuilder
export { browserStreamBuilder, streamBuilder };

0 comments on commit 6837093

Please sign in to comment.