Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSocket on Heroku ERR_CONNECTION_REFUSED

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.

like image 642
user3157307 Avatar asked Jan 16 '16 20:01

user3157307


1 Answers

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.

like image 135
user3157307 Avatar answered Oct 01 '22 06:10

user3157307