Heroku Websockets with Java ECONNREFUSED (Connection refused)
Asked Answered
C

3

12

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
Camden answered 30/5, 2015 at 17:37 Comment(2)
Are you binding your app the the $PORT env var on Heroku? You say you are trying ports between 5000 and 8000. Is that on Heroku?Foretaste
What do you mean by binding the $PORT env var , in the Proc file ? No. Should I ?Camden
C
6

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 !
Camden answered 9/6, 2015 at 7:48 Comment(2)
Hey, I'm deploying my java websocket app on heroku. Everything is fine but I cannot connect to it using java client (which works if deployed on localhost) it gives javax.websocket.DeploymentException: Handshake errorStiltner
I used ws://localhost:8080/Dashboard/hello while on local host and ws://host_name/Dashboard/helloStiltner
F
4

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

Foretaste answered 2/6, 2015 at 14:57 Comment(4)
Thanks , I'll try that . But does it mean I cannot use any other port ? What if I need 3 different ports to listen on ?Camden
Heroku supports one port per application process. Instead of using a different port for the WS, you will need to use a different route in your app. There are third-party tools for exposing auxiliary ports but that might not be suited to this case.Foretaste
I have just tested with the local $PORT environment variable.Didn't make any difference :( See please my update in a question , I can provide more details if needed.Camden
Ok , I've made it ! The interesting part is I didn't have to add any port to my wss connection ! :) It is like so now : "wss://test-websocket-again.herokuapp.com/events/" :)Camden
S
0

You might just need to have REDIS_URL set. It was in REDISCLOUD_URL in my case.

Shiksa answered 2/4, 2021 at 19:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.