I am writing a small game server using Java.
I use TooTallNate-Java-Websockets library to create my websocket server. Everything works when I run my server on localhost , I can connect to it from everywhere.However when I submit my app to Heroku , every time I try to establish a socket connection I get an error ECONNREFUSED (Connection refused).
Worth to mention , that when I am running my app with foreman which is supposed to emulate heroku environment , everything works as it should.
As a port for my websocket server I tried to use 8080 and many others in range between 5000 to 8000.
I can only guess what is going on there on heroku , as logs contain only basic info of http requests.
Please help , I am close to give up :(
EDIT
Here is what I have in my Proc file:
web: ./build/install/my-app/bin/my-app
UPDATE Created a simple abstraction app to showcase the problem: (Tested in foreman of course , and it works in local environment)
WebSocket connection to 'wss://test-websocket-yan.herokuapp.com:39773/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
I am answering my own question to share how I've managed to connect to Java websocket server on Heroku.
You must use the port that Heroku sets as the $PORT
env var:
On Heroku, apps are completely self-contained and do not rely on runtime injection of a webserver into the execution environment to create a web-facing service. Each web process simply binds to a port, and listens for requests coming in on that port. The port to bind to is assigned by Heroku as the PORT environment variable.
For more see: https://devcenter.heroku.com/articles/runtime-principles#web-servers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With