-
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
Version 4.2.0: ws does not work in the browser #1141
Comments
Both libraries are attached with |
The 4.2.0 does change the internal use of ws to use its internal implementation of streams for web sockets. Can you provide a way to reproduce this issue so it can be investigated and fixed?
Get Outlook for iOS<https://aka.ms/o0ukef>
…________________________________
From: Teemu Sirkiä <[email protected]>
Sent: Wednesday, August 12, 2020 10:20:47 PM
To: mqttjs/MQTT.js <[email protected]>
Cc: Subscribed <[email protected]>
Subject: [mqttjs/MQTT.js] Version 4.2.0 doesn't work with mapbox-gl-js (#1141)
After the latest update, my web page using mqtt and MapBox GL JS library fails to fetch resources and gives this error:
Error: ws does not work in the browser. Browser clients must use the native WebSocket object
The error comes from inside mapbox-gl-js library. Does the new version somehow make global changes to some prototypes etc. which would cause this? The error disappeared after downgrading back to 4.1.0.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub<https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fmqttjs%2FMQTT.js%2Fissues%2F1141&data=02%7C01%7Cyoseph.maguire%40microsoft.com%7C10f5c55e2fbe452cdf9e08d83f48a305%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637328928491529238&sdata=OsM4y2ZyCPj%2FT98qXaIpruqx9dcAyoQPxk7cs8WAAhQ%3D&reserved=0>, or unsubscribe<https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAB6VMLHO73YTBVMBHHTYLSTSANZ27ANCNFSM4P5Z5YNA&data=02%7C01%7Cyoseph.maguire%40microsoft.com%7C10f5c55e2fbe452cdf9e08d83f48a305%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637328928491529238&sdata=ZgI3IuZkXRUc%2FQslD7Yqy1Isqn4YXzoD9JlRHTmxotM%3D&reserved=0>.
|
@YoDaMa I'll try to provide a simple example. The problem is with the MapBox authentication token to make it work. |
OK, mapbox-gl-js is not related here. It seems that the unpkg version causes this.
This will cause the error. |
Observing the same issue in a browser app built with Webpack, so it does not seem to be related to
Downgrading to |
@YoDaMa It looks like Line 3 in b2c1215
The problem is that One way to fix this would be to replace |
So unfortunately the testing infrastructure we have didn’t test for browser functionality like I thought it would during the release process.
The changes made were to remove websocket-stream as a dependency, which in turn uses ws as a dependency, and instead simply use ws for performance and reliability improvements. This change seems to have broken webpack though.
Get Outlook for iOS<https://aka.ms/o0ukef>
…________________________________
From: Luca Ongaro <[email protected]>
Sent: Thursday, August 13, 2020 9:08:42 AM
To: mqttjs/MQTT.js <[email protected]>
Cc: Yoseph Maguire <[email protected]>; Mention <[email protected]>
Subject: Re: [mqttjs/MQTT.js] Version 4.2.0 doesn't work from unpkg (#1141)
Observing the same issue in a browser app built with Webpack. Since the 4.2.0 update, the app breaks with:
Error: ws does not work in the browser. Browser clients must use the native WebSocket object
Downgrading to 4.1.0 fixes it. Is ws used also for when using MQTT in a browser environment?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fmqttjs%2FMQTT.js%2Fissues%2F1141%23issuecomment-673567509&data=02%7C01%7Cyoseph.maguire%40microsoft.com%7Cd4ec276751b44c7431b208d83fa32699%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637329317245154402&sdata=aTE6J92LaKBXGfZM04sjBFB903qfIQ%2FMzlwUnqLxoR8%3D&reserved=0>, or unsubscribe<https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAB6VMLHZ6LRXVJZ63KZAN2TSAQFYVANCNFSM4P5Z5YNA&data=02%7C01%7Cyoseph.maguire%40microsoft.com%7Cd4ec276751b44c7431b208d83fa32699%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637329317245154402&sdata=QVjtMnZVN4rSSaJe9iyUy0p7XozT8bLf18YOOwY6EWs%3D&reserved=0>.
|
yeah so like you said we need to implement an isomorphism for the browser compatibility to work with the Websocket in browser builds. I'm working on a PR rn to fix this. |
FYI:
and |
Hi @YoDaMa when the update to solve the bug will be release? Reggards. |
Can someone in this thread try using the patch provided in #1145 . I've tested it manually on my machine and it seems to be working but I'd like other folks trying it out. @pr0x1 @ysfscream @lucaong |
Thanks, I’ll try it tomorrow and let you know. |
Hi @YoDaMa I was testing but the issue continues. i'm using the following script <script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>Regards. |
@YoDaMa 👍 Thanks for the fix. I will test it later. |
really say there is lots of problems with running mqtt in browser. Not only ws, but even if we removed methods that require shiming from mqtt (#1134) we still can't use mqtt.js in browser without hacks because of underlying libs. As a result we need to maintain hacks like that https://github.com/sclausen/ngx-mqtt/pull/158/files really say I don't see how to improve situation, except providing node shims via webpack in each dependant project. Problem with WS is just one of the symptoms. |
@nosovk I am not sure what you mean, I have been using MQTT.js in the browser for years now, connecting via Websocket, and never experienced any problem. Maybe I miss some context, but I really think that supporting browsers is a very important goal for MQTT.js, as that's one of the primary environments where JS runs. |
yes sorry @nosovk I'm a bit confused at what you're getting at |
release 4.2.1 (#1149) fixes this. |
Thanks @YoDaMa for fixing this :) |
@YoDaMa @lucaong sorry for late response, I'le provide some explanation. Current browser version of MQTT.js is build via browserify. Browserify is predecessor of webpack, it's a bundler. What does it mean? When you make browser build of mqtt.js you ship mqtt bundle combined with all deps. For example that one https://github.com/mqttjs/MQTT.js/blob/master/package.json#L76 and other. It's ok, if you add mqtt js like external module in yours application. That bundle is minified and has no external requirements. But some time after Browserify there was Webpack/Rollup was invented. They works in a bit different.way. For webpack good library is not the one which provides compiled\minified js file. For webpack\rollup good dependency is js code, which is not minified and surely not bundled with deps. Rollup will try to treeshacke librarry deps with other packages deps, and split to smaller and probably even lazy loaded chunks. And splitting/minifying should be done according to the project config (not library config). Good example is Angular. Angular requires libraries to provide es, umd, esm etc bundles. Package spec: https://docs.google.com/document/d/1CZC2rcpxffTDfRDs6p1cfbmKNLA6x5O-NtkJglDaBVs/preview Alternate solution - allow Angular to build all needed versions itself. But it's only possible only if source code of the library is ready to be complied by webpack. In case of mqtt it means that nodejs specific methods shouldn't be invoked. For example process etc. Currently mqtt code could be builded with webpack\rollup, but one of dependant libs - is not. webpack\rollup couldn't process nodejs/readable-stream. It will be fixed, but currently all libs that depend on readable-stream have to use browserified (non tree shakable) version or enable node shims (polyfils). For webpack versions prior to 5 it wasn't a big problem because of default webpack configuration have those polyfils enabled. But for Rollup or Angular projects polyfills should be explicitly defined. For example check sclausen/ngx-mqtt@b488b80 Using mqtt with webpack now could be a bit tricky (with webpack 5 probably not possible) |
After the latest update, my web page using mqtt and MapBox GL JS library fails to fetch resources and gives this error:
Error: ws does not work in the browser. Browser clients must use the native WebSocket object
The error comes from inside mapbox-gl-js library. Does the new version somehow make global changes to some prototypes etc. which would cause this? The error disappeared after downgrading back to 4.1.0.
The text was updated successfully, but these errors were encountered: