Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku Websockets with Java ECONNREFUSED (Connection refused)

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)

  1. My html/js tester : Testerpage
  2. My Main java Jetty server : Java Main
  3. Console error message : WebSocket connection to 'wss://test-websocket-yan.herokuapp.com:39773/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
like image 494
Ivelius Avatar asked May 30 '15 17:05

Ivelius


2 Answers

I am answering my own question to share how I've managed to connect to Java websocket server on Heroku.

  1. Create your web socket server using Java , the implementation is up to you , you can use javax or Jetty websockets or TooTallNate-Java-Websockets , I've used Jetty. Here is my implementation . I have followed this example.
  2. When connecting to your web socket on heroku use the following scheme ws:// + yourHerokuAppAdress + yourSocketEndpoint . The endpoint is the relative adress that your websocket is listening on , in my case it is "/socket" . No Need to specify port !
like image 74
Ivelius Avatar answered Oct 18 '22 10:10

Ivelius


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

like image 37
codefinger Avatar answered Oct 18 '22 09:10

codefinger