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!

2
  • 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. Commented Jan 1, 2022 at 3: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 Commented Jan 4, 2022 at 6:10

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.