Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't my app establish websocket connection on Heroku?

I am trying to deploy my nodejs app on heroku. I cannot get the websocket connection to establish. I have read everything I could find, tried every example and none of them seem to work for me.

I when I try to open my page "heroku open", nodejs is correctly giving me the html file. However, the websocket 'ws' connection never establishes.

My server.js has these configurations:

var pport = process.env.PORT || 5000;
server.listen(pport, function(err) {
    if(!err) { console.log("Listening on port " + pport); }
});

Side Note When I check "heroku logs", I find that the port my app is running on is a random 5 digit number. ( like 28896, 53365, etc.) It never actually runs on the second half || 5000.

But the thing is, in order for my game.html file to establish a websocket connection, it needs to know what port. I have tried the following client configurations, none have worked:

1)

var host = location.origin.replace(/^http/, 'ws');
this.connection = new WebSocket(host);

2)

var host = location.origin.replace(/^http/, 'ws');
host = host + ":5000";
this.connection = new WebSocket(host);

3)

this.connection = new WebSocket('ws://infinite-earth-7708.herokuapp.com/');

I have also done what their website said, and attempted to use the following after deploying my app:

heroku config:add NODE_ENV=production

Please advise

like image 348
Captainlonate Avatar asked Nov 03 '13 16:11

Captainlonate


People also ask

How do I fix WebSocket connection failed?

Solution 1Check that all the Bot Insight services are running. Check that your firewall settings are configured to accept incoming websocket data. Try to use a different web browser. Restart the Bot Insight Visualization and Bot Insight Scheduler services.

Can you use WebSockets with Heroku?

It provides a bidirectional channel for delivering data between clients and servers. It gives you the flexibility of a TCP connection with the additional security model and meta data built into the HTTP protocol. For more details on the WebSocket protocol refer to RFC 6455, which is the version supported on Heroku.

How do I know if my WebSocket connection is established?

In the search field, enter websocket . From the search results, click WebSocket Connection Status.


1 Answers

Well I figured it out. Here is what you should know:

  • I did not change my server configurations from my original post.

My client configurations looked like this:

var host = location.origin.replace(/^http/, 'ws');
this.connection = new WebSocket(host);

But here is the kicker. On the terminal I used the following command:

heroku labs:enable websockets

And voila, it works! I hope this helps someone.

like image 175
Captainlonate Avatar answered Nov 10 '22 10:11

Captainlonate