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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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..
@DennisSmink Are you using a port number at the end of the URL? When I removed that from my code, it worked as expected.
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.
What do you mean by you were running 2 nodes?
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!
|
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.

1 Comment

Please avoid posting answer such as 'me too' or 'I am facing the same problem' without providing a solution to that problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.