error log in console browser : "WebSocket connection to 'ws://127.0.0.1:1883/mqtt' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET"
my code .js to connect mosquitto server:
var options = {
clientId: 'web-client',
connectTimeout: 5000,
hostname: '127.0.0.1',
port: 1883,
path: '/mqtt'
};
var client = mqtt.connect(options);
use library mqtt-2.9.0.js
use mosquitto v1.5.4 windows10
=========================================
By default Mosquitto listens on port 1883 and accepts connections using native MQTT
If you want to connect with MQTT over Websockets you need to configure Mosquitto to listen on a different port and specify to use the websockets transport.
You can add the following to your mosquitto.conf:
listener 8883
protocol websockets
This will cause mosquitto to listen on port 8883 for MQTT over Websockets conections.
You can then modify your code as follows:
var options = {
clientId: 'web-client',
connectTimeout: 5000,
hostname: '127.0.0.1',
port: 8883,
path: '/mqtt'
};
var client = mqtt.connect(options);
It's also worth pointing out that your clientId needs to be unique for EVERY client that connects, so you will need to make it dynamic if you are going to load the page more than once at a time.
While 1883 is the usual port for vanilla MQTT connection - the usual default for websockets is port 8883. Have you tried port 8883?
Durr edited my typo 8888 to 8883
Related
I am trying to get a simple mqtt broker set up and access it from a web page. I have had pretty much 0 luck.
I've got mosquitto 2.0.14 downloaded and running. Here's my configuration file:
listener 1883
listener 9001
protocol websockets
This generates the following log when I run mosquitto -c mosquitto_conf -v
1637948154: mosquitto version 2.0.14 starting
1637948154: Config loaded from mosquitto.conf.
1637948154: Opening ipv6 listen socket on port 1883.
1637948154: Opening ipv4 listen socket on port 1883.
1637948154: Opening websockets listen socket on port 9001.
1637948154: mosquitto version 2.0.14 running
Here's my html file, which I simply open in the browser. It uses Paho's js client.:
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
var mqtt;
var reconnectTimeout = 2000;
var host = "192.168.1.94";
var port = 9001;
function onConnect() {
console.log("Connected");
message = new Paho.MQTT.Message("hello");
message.destinationName = "sensor1";
mqtt.send(message);
}
function mqttConnect() {
console.log("Connecting to " + host + ":" + port);
mqtt = new Paho.MQTT.Client(host, port, "clientjs");
var options = {
timeout: 3,
onSuccess: onConnect,
};
mqtt.connect(options);
}
</script>
</head>
<body>
<script>
mqttConnect();
</script>
</body>
</html>
I'm using a guide from this website: http://www.steves-internet-guide.com/using-javascript-mqtt-client-websockets/
It errors out with the following console error in the browser:
WebSocket connection to 'ws://127.0.0.1:9001/mqtt' failed
I have been having a hard time finding an updated tutorial that works. My ultimate goal is to create a react app that connects to an mqtt broker via websockets and receives messages to update state in redux.
Questions:
How do I get the js client to connect?
How do I set the host for mosquitto? Can I use a diff host like myhost.local or am I stuck using 127.0.0.1 or whatever I see when I run ipconfig (I'm on windows)?
You need to add allow_anonymous true to allow users to connect without supplying a username/password.
This is part of the set of changes introduced in v2.0 to improve the default security posture of mosquitto out of the box.
I have a problem with PeerJS server. I used "Deploy to Heroku" button from here:
https://github.com/peers/peerjs-server
I have no idea how can I connect with deployed cloud.
I can't find clear documentatnion about PeerJS Server.
I don't know what is the host, port, and path for my app.
var peer = new Peer('someid', {host: 'localhost', port: 9000, path: '/myapp'});
Please advice.
This how it worked for me:
var peer = new Peer('someid', {
secure: true,
host: 'your-app-name.herokuapp.com',
port: 443,
});
Your host is simply the web address to your Heroku app. For instance, if your Heroku app is named peerjsapp, then host would be 'peerjsapp.herokuapp.com'. You can find the name of your app on your Heroku dashboard. The port is usually 9000, but can be 443 if you're using HTTPS (make sure to also pass in secure:true if you're using HTTPS). You don't need to include the path unless you've changed it; if you're running the default server config, leaving out the path on your client will automatically connect. Finally, since you're hosting your own server, you don't need an ID.
• This is how I think you should do it:
const myPeer = new Peer(undefined, {
secure: true,
host: '0.peerjs.com',
port: '443'
})
• EXPLANATION:
After deploying your app to Heroku, typed 'peerjs' into the console to search for the peerjs object, from which you can navigate and find the key-value pair of
CLOUD_HOST: "0.peerjs.com"
CLOUD_PORT: "443"
The next step is just to match your own host and port with these values.
This is how I do it Console Screenshot
• NOTE:
For the secure: true part I have tried and the app works both with and without it. So it's on you to choose to include it or not. I have also found out on https://peerjs.com/docs.html this same information, check it out if you want more detailed documentation.
I want to transfer REST requests from my front end wepp app to the API on a external Jira server.
For this I'm using node http-proxy, which has been ok for a Jira server that is http.
But now I want to create a separate server for https.
So making som changes to this example I now have this:
var path = require('path'),
fs = require('fs'),
httpProxy = require('http-proxy'),
certFolder = '/my/cert/folder';
//
// Create the HTTPS proxy server listening on port 8002
//
httpProxy.createServer({
//(placeholder address)
target: {
host: 'https://ext.jiraserver.com',
port: 443
},
// letsencrypt cert
ssl: {
key: fs.readFileSync(path.join(certFolder, 'privkey.pem'), 'utf8'),
cert: fs.readFileSync(path.join(certFolder, 'fullchain.pem'), 'utf8')
},
secure: true
}).listen(8002);
console.log('https proxy server started on port 8002');
But when I make a request to my server, https://my.domain.com:8002 it crashes the server with error message:
.../node_modules/http-proxy/lib/http-proxy/index.js:119
throw err;
^
Error: getaddrinfo ENOTFOUND https://ext.jiraserver.com
https://ext.jiraserver.com:443
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
I can't seem to get it to work... The server is online and the address is correct, so I don't know what's wrong.
Is it my code that's wrong or what can I do to get this to work?
Thanks!
Don't include the https:// in a DNS host definition.
host: 'ext.jiraserver.com',
The error message tells you that it's a DNS resolve problem. You're trying to lookup the DNS of https://ext.jiraserver.com including the https.
I am trying really hard to make my MQTT client work inside my application. The broker is provided by CloudMQTT.
When trying to run the following code in NodeJS, the client connects properly to MQTT;
var mqtt = require('mqtt');
var client = mqtt.connect(
"mqtt://m20.cloudmqtt.com",
{
port: 11212,
username: "XXXXXXXX",
password: "XXXXXXXX"
}
);
client.on('connect', function () {
console.log('connected');
});
However, when I run the same code (without the require of course) in the frontend using the Bower package, the client does not connect. I have also tried other MQTT browserfied JS packages. I prefer MQTT.js and not Paho, because I would like to use multiple subscribes with one connected client.
If console.log(client);, NodeJS uses protocol: 'mqtt' and protocolId: 'MQTT'. The frontend uses protocol: 'ws' and protocolId: 'MQTT'. Could this be the problem? Adding these options to mqtt.connect has no effect.
From with in the browser the only option is going to be to connect via MQTT over Websockets. CloudMQTT use different port numbers for native MQTT and MQTT over websockets so you will need to also change the port number for the browser based code to the Websocket port listed in your CloudMQTT dashboard.
I have purchased a certificate and installed in my node.js website.But the https at the browser shows green and is OK.Now, I am trying to establish a socket connection using wss, but it failed.
The error at the Javascript client side is like this.
WebSocket connection to 'wss://securedsitedotcom:3003/call' failed:
WebSocket opening handshake was canceled
Please help!
Code at client side (Javascript)
var ws = new WebSocket('wss://securedsitedotcom:3003/call');
Code at server side (node.js)
https = require('https');
var server = https.createServer({
key: fs.readFileSync(config.certKeyPath),
cert: fs.readFileSync(config.certCrt),
requestCert: true,
rejectUnauthorized: false
},app);
server.listen(port);
var wss = new ws.Server({
server: server,
path: '/call'
});
Error at the browser console :
WebSocket connection to 'wss://securedsitedotcom:3003/call' failed:
WebSocket opening handshake was canceled
Recent work with Chrome has revealed that if a page is served as https on Chrome, websockets must use wss. And if wss must be used, port 443 must be used (and to boot not any other secure port and so far I have not seen any way to change the port), which may be your problem since your port looks like 3003 above.
Right now I am trying to get my IT group to patch/upgrade Apache on that server so mod_proxy_wstunnel can be used to make Apache listening on 443 a reverse proxy and pass all wss traffic through to my websocket server.
Hope this helps.
I ran into a similar issue, but I was using a Self Signed Certificate. You mentioned that you bought a Certificate. I guest it is signed by the certificate authority.
Otherwise, like in my case, non-validated certificate can cause an "opening handshake was cancelled" error. A validated certificate is either validated by a third party (Certificate Authority, ie VeriSign) or explicitly authorized by the client.
In my case, VeriSign didn't sign my certificate (self signed), so I had to explicitly authorized it. To do so, I simply need to visit the https URL with the browser (in your case "https://securedsitedotcom:3003/call"). Then a "warning unauthorized host" appear. You need to authorize the exception and than you can make your WSS connection.
Your server can use any port, it is not bound to 443. 443 is the default port for https but any port can be explicitly specified like you've done.
I hope it helps someone.