0

I followed this post and I created this below:

const express = require('express');
const https = require('https');
const WebSocket = require('ws');
const fs = require('fs');

    var privateKey  = fs.readFileSync('key.pem', 'utf8');
    var certificate = fs.readFileSync('cert.pem', 'utf8');
 
    var credentials = {key: privateKey, cert: certificate};
   
    var app = express();
    
    var httpsServer = https.createServer(credentials, app);
    httpsServer.listen(3000);

    var WebSocketServer = require('ws').Server;
    var wss = new WebSocketServer({
        server: httpsServer
      });

    wss.on('connection', function connection(ws) {
      ws.on('message', function incoming(message) {
        console.log('received: %s', message);
      });

      ws.send('something');
    });

Running this seems fine on the surface, but then when I try to connect to said websocket on the same ec2 instance with ws = new WebSocket('wss://(the private or public Ip, or the domain for my website, I tried all of them):3000'; and I get the error: WebSocket connection to 'wss://stuff' failed:

Are there additional logs I could look at that might reveal the problem, maybe an additional step I need because of how I'm trying to set this up on an ec2, or is there just something plain wrong with what I did here? Thanks!

  • at first glance i would say move the `server.listen` to after `wss.on` call as it appears the code here seems correct. If that doesn't help it could be your certs. You might also try to use the `wss.on('error')` event handler to see if that gives more information. – about14sheep Jan 01 '22 at 03:56
  • Sorry, I just realized I forgot to reply. Yeah I added the server.listen after the wss.on and I found out that the problem was with my certificate, thanks – Theodore Nickolov Jan 04 '22 at 06:10

0 Answers0