2

I try to make a P2P chat using websocket.

Locally, the installation works, but in production, the console shows this error message:

WebSocket connection to 'wss://mysite.com/8080' failed: Error during WebSocket handshake: Unexpected response code: 404

Here is the code of the local server, which works:

const Socket = require("websocket").server
const http = require("http")

const server = http.createServer((req, res) => {})

server.listen(8080, () => {
    console.log("Listening on port 8080")
})

const webSocket = new Socket({ httpServer: server })

and the production

const Socket = require("websocket").server

const https = require("https")

const server = https.createServer((req, res) => {})

server.listen(8080, () => {
    console.log("Listening on port 8080")
})

const webSocket = new Socket({ httpServer: server })

Also the JS code

 const webSocket = new WebSocket("ws://127.0.0.1:8080")
 const webSocket = new WebSocket("wss://mysite.com/8080")

What am I doing wrong please?

Christian
  • 4,902
  • 4
  • 24
  • 42
  • Why is your path `/8080` in production. This looks like a bug – Evert Oct 09 '22 at 14:07
  • It's not a bug, I tought is was the right path. The server is listening on this port on localhost. To make it work online I changed the first part of the path by adding my website address, but what should be following, if not the port? – user20197852 Oct 09 '22 at 21:01
  • Is your app actually https? Wss means secure which i doubt you are doing – Asad Awadia Oct 09 '22 at 23:00
  • @user20197852 anything after `/` is not a port. The host and port is separated with a `:` – Evert Oct 10 '22 at 01:54
  • Asad Awadia Yes my site is https. Evert I tried with a : then with a / but result is the same. – user20197852 Oct 10 '22 at 10:29

1 Answers1

1
ws://127.0.0.1:8080
^^            ^

wss://mysite.com/8080
^^^             ^

See the difference? On the local server you used ws which means unencrypted websocket. On the remote server you used wss which means websocket with HTTPS. If your server isn't set up to handle encrypted HTTPS connections then the connection will fail.

Additionally, the port number comes after : - by writing / you said to use the default port number, which is 443 for HTTPS and 80 for HTTP, and the /8080 is the path, which I think your server doesn't care about.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • in my situation this isnt based on this see my question : https://stackoverflow.com/questions/76748567/cant-connect-to-websocket-server – AlirezaBest Jul 31 '23 at 18:57