5

I am trying to run a node js app on Heroku using WebSockets. However, I am not able to resolve this error (As seen in conosle of Chrome browser)

WebSocket connection to 'wss://myappname.herokuapp.com:27225/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

I am using 'wss' since Heroku runs on HTTPS.

My client side code is :

$.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 appreciated.

Thank You.

Mykola
  • 3,343
  • 6
  • 23
  • 39
user3157307
  • 186
  • 1
  • 7

2 Answers2

11

So it seems like I was trying to over-do it with the port number. Just using the host as wss://myappname.herokuapp.com/ works well.

user3157307
  • 186
  • 1
  • 7
  • I am having the exact problem you expierenced, how did you managed to get it up? I keep getting the ERR_CONNECTION_REFUSED error. My code is nearly identical to yours.. – Dennis Smink Mar 04 '16 at 14:10
  • @DennisSmink Are you using a port number at the end of the URL? When I removed that from my code, it worked as expected. – user3157307 Mar 05 '16 at 15:03
  • I removed the port, but did not seem to work. Eventually I resolved it, I was running 2 nodes instead of 1, heroku doesn't allow this. – Dennis Smink Mar 05 '16 at 15:33
  • What do you mean by you were running 2 nodes? – jhnferraris Jun 27 '16 at 03:58
  • That's weird, why we shouldn't mention the port number when using this library, Thank's though, you saved me a lot of time! – arbel03 Jan 12 '17 at 19:12
  • I have been hitting myself over the head trying to figure out why when I deployed my app to Heroku the connection was being refused due to an SSL error... this fixed it!! – michjo Nov 27 '18 at 04:54
0

I also found this problem. It seems Heroku will automatically route port number. It does's allow to specify port number in url. In my chrome, it show ERR_CONNECTION_RESET. This also happen with XMLHttpRequest. Port number still need when you test with localhost or another server which is not Heroku.

  • Please avoid posting answer such as 'me too' or 'I am facing the same problem' without providing a solution to that problem. – Matus Dubrava May 19 '18 at 17:10