Hi sorry my first stack overflow question so I'll do my best. I've read through dozens of related questions but none seem to provide me with my answer.
I've recently started working with Node.js as I need websockets for some functionality on my website and Node.js seemed to be the popular choice. My website has an SSL Certificate through LetsEncrypt so I've added the cert and key files to my WSS configuration file 'index.js' as shown below:
const fs = require("fs");
const https = require("https");
const WebSocket = require("ws");
const server = https.createServer({
cert: fs.readFileSync("/etc/letsencrypt/live/my_domain.com/cert.pem"),
key: fs.readFileSync("/etc/letsencrypt/live/my_domain.com/privkey.pem"),
requestCert: true,
rejectUnauthorized: false
});
const wss = new WebSocket.Server({ server });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
});
server.listen(8080, () => console.log("Listening on port :8080"))
When I node index.js on my server through the terminal I get no errors and it outputs: 'Listening on port :8080' as expected. When I then navigate to a simple index.html page I made to test this was working with the following code:
<script>
const socket = new WebSocket("wss://my_domain.com:8080");
socket.addEventListener("open", function (event) {
console.log("We are connected!");
ws.send("Hi! I've connected!");
});
socket.addEventListener("message", function (event) {
console.log(e.data);
});
</script>
I just get the following console error in browsers:
Firefox:
Firefox can’t establish a connection to the server at wss://my_domain.com:8080/.
Chrome:
WebSocket connection to 'wss://my_domain.com:8080/' failed: (anonymous) @ index.html:60
I can't see any other details in the Network inspector tools but if there's anything I can provide for extra clarity/context please ask and I'll respond asap.
EDIT: This article was suggested but does not seem to fix the problem: in their question it works in chrome but not in firefox, mine has yet to work in any browser. Also in the answers on that post they suggest going to a 'normal page on the same server' to accept the certificate but I can't seem to trigger a dialogue box on any of my pages.
I also think my certificate is NOT self-signed as all these posts seem to be but I honestly don't know:
Screenshot of certificate info