WebSocket on Heroku ERR_CONNECTION_REFUSED

I am trying to run a node js application on Heroku using WebSockets . However, I cannot resolve this error (as seen in the conosle of the Chrome browser)

Connection to WebSocket with 'wss: //myappname.herokuapp.com: 27225 /' failed: connection error: net :: ERR_CONNECTION_REFUSED

I use 'wss' since Heroku runs on HTTPS.

My client code:

 $.get("https://myappname.herokuapp.com/port",function(data){ port = data; console.log(data); host = 'wss://myappname.herokuapp.com:' + port + '/'; ws = new WebSocket(host); }); 

My server side code is:

 var WebSocketServer = require("ws").Server var fs = require('fs'); var express = require('express'); var app = express(); var http = require('http'); var port = process.env.PORT || 5000; var request = require('request'); var server = http.createServer(app); var serverOnPort = server.listen(port); console.log("Server listening on port ",port); var wss = new WebSocketServer({server: serverOnPort}); console.log("websocket server created"); 

Any help would be greatly appreciated.

Thanks.

+5
source share
1 answer

So it looks like I was trying to redo it with the port number. Just using the wss://myappname.herokuapp.com/ works well.

+6
source

Source: https://habr.com/ru/post/1240770/


All Articles