1

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

James Z
  • 12,209
  • 10
  • 24
  • 44
  • Does this answer your question? [Can not established Websocket secure connection on Firefox](https://stackoverflow.com/questions/23775215/can-not-established-websocket-secure-connection-on-firefox) – James Jul 05 '21 at 14:07
  • @James No- For one 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. – LithuanianGrass Jul 05 '21 at 14:10
  • It might indicate the problem, though - that your browser will not accept the SSL cert on your server when accessing a websocket endpoint. All the google searches I did on that error message are pointing to the cert. – James Jul 05 '21 at 14:13
  • @James all those articles refer to self-asigned certificates. To be completely honestl I don't know too much about SSL certs but I've just checked my certificate details and it shows the Issuer as Let's Encrypt. Doesn't this mean it's not self-asigned and so all these answers are irrelevant? – LithuanianGrass Jul 05 '21 at 14:22
  • Let's encrypt isn't self-signed. Do you get a similar error accessing `https://my_domain.com:8080/` in a browser? – James Jul 05 '21 at 15:00
  • hey @James thank you for the continued help. For clarity the domain name is geotrainr.com so if you want to take a look yourself you might know where to look better than i do, but if i go to https://geotrainr.com:8080/ i just get site can't be reached, ERR_CONNECTION_TIMED_OUT. – LithuanianGrass Jul 05 '21 at 15:17

1 Answers1

1

After hours of trying to find complicated answers to complicated questions it ended up being a simple firewall issue so thought i'd come back and post what fixed it for me incase anyone else runs into a similar issue in the future.

I simply ran the following command on the server:

 ufw allow 3000/tcp

Good luck everyone :)