Search code examples
javaherokujava-serverjava-websocket

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

Solution

  • 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 !