Heroku Websockets with Java ECONNREFUSED (Connection refused) - java

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)
My html/js tester : Testerpage
My Main java Jetty server : Java Main
Console error message :
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.
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.
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 !

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

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

Related

CloudFoundry websocket failed: Establishing a tunnel via proxy server failed

Note: I am not using Pivotal CF.
I have a java application deployed on CloudFoundry. I am using embedded Jetty to host my Jersey REST API. This API is by default exposed on port 8080 by cloud foundry.
My application also needs some websockets to stream data to the browser. I am using Java-WebSocket (https://github.com/TooTallNate/Java-WebSocket) for this. On my local machine, I was using port 8887 for my websocket connection. Everything worked fine.
After deploying on CloudFoundry, I can access my REST API but not my websocket. After searching a bit online, I found that websocket connections are only allowed on port 4443 (http://docs.run.pivotal.io/release-notes/)
I changed my server side to reflect this
import org.java_websocket.server.WebSocketServer;
public class MyWebSocket extends WebSocketServer {
public MyWebSocket() throws UnknownHostException {
super(new InetSocketAddress(4443));
}
#Override
public void onOpen(org.java_websocket.WebSocket websocket, ClientHandshake handshake) {
// Handle this
}
}
On my client side, I am connecting the websocket using the following
wss://my_cf_app.com:4443/
But I am getting the following exception.
WebSocket connection to 'wss://my_cf_app.com:4443/' failed:
Establishing a tunnel via proxy server failed
I also tried to connect the websocket on server side using "PORT" environment variable of CF but I get "Address already in use" error in Java-WebSocket.
I have tried many different things but I am unable to figure this out. Any help would be awesome.
After deploying on CloudFoundry, I can access my REST API but not my websocket. After searching a bit online, I found that websocket connections are only allowed on port 4443 (http://docs.run.pivotal.io/release-notes/)
Port 4443 is specific to Pivotal Web Services (and some installs of CF that run on AWS). Most PCF installs do not have a separate port for WSS, but just use 443 along with the HTTPS traffic. The port used ultimately depends on the load balancer being used in front of the CF installation and what it supports.
You would never have your application listen on port 4443. Port 4443 is the external port for traffic where the load balancer listens. This traffic will be directed to the port assigned to your application, which is $PORT (env variable).
I also tried to connect the websocket on server side using "PORT" environment variable of CF but I get "Address already in use" error in Java-WebSocket.
This is the correct behavior, i.e. you should be listening on the port assigned through $PORT env variable. What the error is telling you is that something is already listening on this port and you cannot have two things listening on the same port.
There is only one port available per application at this time (likely to change in the future). For now, if you have two separate applications listening on two separate ports then you need to push them to CF as two separate applications.
What you can do to make them appear like one application to end users is to map each one to a specific path. See the --route-path argument of cf push or docs for cf create-route.
https://docs.cloudfoundry.org/devguide/deploy-apps/routes-domains.html#create-route

unable connect to java app running on server (Heroku)

I deployed my app on Heroku. It is a simple app that will throw back the message sent from its client version (running on my PC) back to the client. My server app prints the IP address of the machine it's running on so I can connect to it from my client . I do get to see the IP address of Heroku machine when I deploy and run it. However, I am not able to connect to it from my client version using that IP address(Connection timed out) . I have allowed the port I am using in my firewalls. I don't understand what I am doing wrong .
Pls help me solve it.
If you need to see my code let me know ( the app works fine locally on different terminals so I don't think it has a problem).
Your Heroku dyno (an isolated server) is behind a router. You can't use the IP address to connect to it.
Instead, you the URL https://[appname].herokuapp.com where "[appname]" is the name of your app.
You can also run heroku open from the command line.

Java server on OpenShift

At the moment i have an android client app which connects to my java server through socket - serversocket. It sends and receive strings. The java server is connected to a mysql database (actually mariadb) using the jdbc driver.
I succeed to create a jbossas application and upload the code of the java server to openshift, but i didn't find any detailed tutorial on how do i connect to this new uploaded server from my socket client (This one (RMI or socket connection to Java Program on OpenShift) gives some tips but i'm still stucked).
More on this, how do i know that my server runs just fine on openshift and how do i control de calls to the database after i connect it (found this: $ rhc app create MyApp jbossas-7
$ rhc cartridge add mysql-5.5 -a MyApp), using org.mariadb.jdbc.Driver and java.sql is still working ?
Any small guide or tip is highly appreciated. I'm new to these things so please don't be too heavy on comments.
You can only make connections to your OpenShift server on http/https or ws/wss ports. If you want to connect to your java application and pull data from it from an android device, I would suggest using a RESTful api or a servlet, etc.
I had similar problem: My app server originally was running as a ServerSocket listener, and any clients/devices connect to it directly via Socket binding.
To deploy it into OpenShift, my previous initial solution was to change its host:port configuration by following the suggestion as described in this link [Socket connection to Java Program on OpenShift]. It worked nice as far as my app server was successfully up and running. But it did not work well with the port forwarding approach in order to accept remote requests.
So for the final solution, I modified the app server by wrapping my original code with a RESTful webservice around it, and deploy it as a web service.

Could not connect to ActiveMQ service running on AWS - EC2

I am trying to connect to aws-ec2 instance having activemq service running. I am trying to connect it using tcp transport connector using an application built under java + maven + git setting.
I am doing this exercise to add network latency to compare activemq with aws-sqs, which will help us in deciding which one to use for our requirements.
what I have tried so far:
I am able to run this application locally as I scp'ed into ec2 added my code there and it ran locally, which led me to believe that amq service is running.
I also ran the same setting from my home network it is working fine, so home computer -> ec2 connection is also working.
It is just not working from my office network, which led me to believe that it is failing because of some kind of firewall setting?
A simple netstat -a | grep 61616 gave me that it is listening on port 61616.
Does anyone know where I am going wrong? Or suggest some more debugging techniques?
EDIT: Security rule at aws-ec2

JBoss Binding IP addresses

I need some help from someone who understands JBoss Hostname Binding. I think the solution is easy, although it's complicated to explain.
I am deploying an application using JBoss (v4.2) and am having troubles configuring the application.
This application has two parts, a web site on port 8080 and web services on port 8080 using SOAP APIs.
My server sits behind a firewall, and has an alias, let's say it's called orange.mycompany.com
My problem is that I cannot get the console to connect to the web services. The website works, but I see an connection refused error connecting to the web services.
[xfire.transport.http.HttpChannel]
java.net.ConnectException: Connection
refused
There are 2 things I can control, the bind IP on Tomcat, and the URL of the web services.
If I start JBoss, and bind to the local IP address:
./run.sh -b 10.1.2.3
And I set the URL of the web services to be that same IP
url=http://10.1.2.3:8080/services
I can see the website on port 8080 from outside the firewall, but the console cannot connect to webservices.
From the server, orange, itself I cannot see the website by calling http://localhost:8080/ or http://10.1.2.3:8080 or orange.mycompany.com:8080
However, if I start JBoss and bind to 127.0.0.1:
./run.sh -b 127.0.0.1
And I set the URL of the web services to localhost
url=http://localhost:8080/services
Now I can't see the website at all from outside the firewall.
But from the server itself, I can see the website browsing http://localhost:8080 and the I can successfully connect to the web services. That's great, but I need the website to be accessible from outside.
Can anyone suggest any combination of settings that will let me browse the website and also let the console call webservices on localhost?
Never mind.
Start JBoss binding to all IP's works.
./run.sh -b 0.0.0.0
Aren't your running JIRA standalone, right?
I always run tomcat and jboss behind a apache with mod_jk. This still hide ports, what sounds great for newbies users.
Your server is behind a NAT?
Sometimes I use ProxyPass or RewriteRules (mod_rewrite) to provide external access, thru reverse proxy.

Categories