Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The exception 'ws does not work in the browser' if I try connecting from a worker thread #1709

Closed
a1leks opened this issue Oct 6, 2023 · 13 comments · Fixed by #1711
Closed

Comments

@a1leks
Copy link

a1leks commented Oct 6, 2023

The Issue

I use MQTT 5.1.0 in a browser inside a worker thread, but I can't connect and I get an exception: ws does not work in the browser

Screenshot 2023-10-06 at 13 16 12

As I see this code can be the root reason of issue:

const isBrowser =
typeof window !== 'undefined' && typeof window.document !== 'undefined'

I have isBrowser false because I don't have the window object inside a worker. It leads to executing streamBuilder and then I get mentioned exception in createWebSocket

const streamBuilder: StreamBuilder = (client, opts) => {

however I expect executing browserStreamBuilder
const browserStreamBuilder: StreamBuilder = (client, opts) => {

In case I fix the browser checking, everything works fine for me, and the connection is successful.

The proposal/question

Could you consider improving the 'is-browser' checking or giving me an advice on how to properly make MQTT connecting from a browser worker thread? I appreciate your answer.

@expcapitaldev
Copy link

do not see any info about Web worker and MQTT, is it supported ?

@robertsLando
Copy link
Member

import * as mqtt from 'mqtt/dist/mqtt'

@a1leks
Copy link
Author

a1leks commented Oct 9, 2023

@robertsLando

I import exactly like you wrote. It works fine from main JS thread but it doesn't work from web worker JS thread. I've read the doc. Your advise unfortunately didn't help.

Have you ever tested this lib inside worker? Could you clarify is mqtt.js 5.x.x compatible with WebWorkers in browser (or not)? Thanks for answer!

@robertsLando
Copy link
Member

@a1leks any hints about the error thrown?

@a1leks
Copy link
Author

a1leks commented Oct 9, 2023

I run my app in chrome

  1. install mqtt 5.1.0 - npm i [email protected] --save-exact
  2. Create new worker thread using new Worker('./my-worker', {type: 'module'});
  3. Import mqtt inside my-worker.ts using import * as mqtt from 'mqtt/dist/mqtt';
  4. Do connect also inside my-worker.ts using code:
mqtt.connect(
    this.brokerUrl, 
    {
        defaultProtocol: 'wss',
	protocol: 'wss',
	username: this.userName,
	password: this.password,
	clientId: this.deviceId,
    }
);

And then I encountered an error during the connection process (screenshots below):

Screenshot 2023-10-09 at 17 14 58 Screenshot 2023-10-09 at 17 20 03

There is no window in worker.
Screenshot 2023-10-09 at 17 20 51

I can't provide my code, but if there isn't enough information, I can prepare a code sample. Just let me know if it will be useful.

@robertsLando
Copy link
Member

what you could try is to manually create your websocket by using the createWebsocket option

@robertsLando
Copy link
Member

robertsLando commented Oct 9, 2023

something like:

mqtt.connect(
    this.brokerUrl, 
    {
	    defaultProtocol: 'wss',
		protocol: 'wss',
		username: this.userName,
		password: this.password,
		clientId: this.deviceId,
		createWebsocket(url, [protocol], opts) {
				return new WebSocket(url, [protocol])
		}
    }
);

@a1leks
Copy link
Author

a1leks commented Oct 9, 2023

Thanks! I've tried. However in this case I have an error ws_1.default.createWebSocketStream is not a function:

Screenshot 2023-10-09 at 18 05 38

It looks like code from https://github.com/websockets/ws which doesn't work in browser.

This is the root issue I guess. Could you please have a look

const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';

It doesn't look compatible with web workers in browser.

@robertsLando robertsLando reopened this Oct 9, 2023
robertsLando added a commit that referenced this issue Oct 9, 2023
@robertsLando
Copy link
Member

Could you test this? #1711

@a1leks
Copy link
Author

a1leks commented Oct 9, 2023

The fix works good for me. Connection is successful from a web worker.

@robertsLando
Copy link
Member

Perfect, will merge and release tomorrow a new patch

@a1leks
Copy link
Author

a1leks commented Oct 11, 2023

Thanks!

@robertsLando
Copy link
Member

FYI starting from 5.2.1 you can now import mqtt using:

import mqtt from 'mqtt'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants