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
wss://url:8084/mqtt in node.js worked but in browser using webpack failed #741
Comments
You can't use the key, cert, and ca in the browser. You need to trust that certificate manually in the browser, otherwise the secure websocket connection will fail. Feel free to send a PR to update the README with this info! |
@mcollina So grateful for your answer! But there is still some questions about this.
|
@Qtianv587 The browser version of MQTT.js uses the built-in Websocket implementation, which means it uses the Browser's TLS support. Browsers don't provide any APIs to developers that allow custom certificate chains, you have no choice but to use certificates that are signed by an authority that's trusted by the browser. If getting a valid certificate is a problem, I would advise using Letsencrypt |
@RangerMauve @mcollina First we need generate the And then add the certificate OK, next is my trick. I add the following codes before the My purpose at first is trying to generate a websocket between mqtt and browser. But by executing the |
I'm honestly not sure. Could you post some code so that I can see in detail? |
@RangerMauve var WS = require('ws');//ws is a module original from Node
module.exports = {
wsConnect: function (url, protocols) {
return new WS(url, protocols);
}
}; And then I use webpack to transfer this file to another JS file which could be executed in H5.
I have detected ever that the I 'required' the browWS.js file into environment using <script src = 'browWS'>, and then is the code of the whole connection to mqtt. var client;
var options;
var broker = "wss://host:8084/mqtt";
var user;
//**!!!!!****The browser will notice me to import the certificate when execute this function.**
browWS.wsConnect(broker, ["mqtt"]);
function connect(browBuffer, mqtt, username, password, callback) {
user = username;
options = {
'username':username,
'password':password,
'clientId':genID(username),
'retain': false,
'protocol': "wss",
'passphrase': password,
'rejectUnauthorized': false
};
client = mqtt.connect(broker, options);
} That's the whole steps. The |
I'm not sure you need to use wsConnect here, maybe you can get away with using However, I must warn you that self-signed browser certificates are vulnerable to man-in-the-middle attacks, so you should not trust them if you're planning on using any public wifi, or if you can't trust your Internet Service Provider (most of the time you shouldn't) |
This is an automated message to let you know that this issue has If this issue is still important, you can simply comment with a Thank you for your contribution. |
This issue was automatically closed due to inactivity. |
I used mqtt.js in node.js and I used wss as protocol. I used the self-signed certificates to connect to mqtt. In node.js, it worked. So I browserify the MQTT.js with Webpack, and it failed.
The error log shows in Chrome's console is:
WebSocket connection to 'wss://url:8084/mqtt' failed: WebSocket opening handshake was canceled.
And it will remain reconnecting and show this error.
The code in Chrome is:
this is same as I write in node.js:
It's so confused and could someone else also suffered this problem and had solved? I'm here begging your help!
:-(
The text was updated successfully, but these errors were encountered: