-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Comments
do not see any info about Web worker and MQTT, is it supported ? |
|
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! |
@a1leks any hints about the error thrown? |
I run my app in chrome
And then I encountered an error during the connection process (screenshots below): ![]() ![]() 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. |
what you could try is to manually create your websocket by using the |
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])
}
}
); |
Thanks! I've tried. However in this case I have an error ws_1.default.createWebSocketStream is not a function: ![]() 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
It doesn't look compatible with web workers in browser. |
Could you test this? #1711 |
The fix works good for me. Connection is successful from a web worker. |
Perfect, will merge and release tomorrow a new patch |
Thanks! |
FYI starting from 5.2.1 you can now import mqtt using:
|
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
As I see this code can be the root reason of issue:
MQTT.js/src/lib/is-browser.ts
Lines 1 to 2 in 5006aea
I have
isBrowser
false
because I don't have thewindow
object inside a worker. It leads to executingstreamBuilder
and then I get mentioned exception increateWebSocket
MQTT.js/src/lib/connect/ws.ts
Line 141 in 5006aea
however I expect executing
browserStreamBuilder
MQTT.js/src/lib/connect/ws.ts
Line 158 in 5006aea
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.
The text was updated successfully, but these errors were encountered: