WebSockets with Railway

OOliver_5/24/2023
Hi there! So Im doing a game using websockets. Frontend is using react.js and backend is using java with maven.

We have the server up and runing without issues. But we cant seem to get our ports to match, it just says that we fail to connect for exampel port 8080.

We have tried to copy the domain from the backend with :8080 to the frontend.
OOliver_5/24/2023
e929f490-a8af-4200-97d4-e6e331581ff3
Bbrody5/24/2023
your websocket server should run on host 0.0.0.0 and the auto generated PORT (not 8080)
your react frontend will then connect over wss without specifying any port
OOliver_5/24/2023
Hi Brody, We will try it
OOliver_5/24/2023
Should there still be wss://0.0.0.0 ?
Bbrody5/24/2023
wss://<whatever your railway domain is>/<whatever your websocket endpoint is>
OOliver_5/24/2023
So it would be wss://backend-production-6ad2.up.railway.app/

The domain would be from our backend (java) Railway app
Bbrody5/24/2023
if the root endpoint / handles websocket connections and not something like /ws then yes that is correct
Aangelo5/24/2023
For the record, we recommend adding a custom domain for websockets
Aangelo5/24/2023
Railway proxy does terminate sessions after 30 seconds for scale reasons on *.up.railway.app domains
OOliver_5/24/2023
We have now tried to remove the port from our websocket server, So the constructor for the jetty websockets get nothing. And the Procfile gets just PORT. And we still have the issue.
Aangelo5/24/2023
Going to need some logs my guy
OOliver_5/24/2023
We have a custom domain for the future
OOliver_5/24/2023
Sure thing!
OOliver_5/24/2023
Backend Deploy logs:

2023-05-24 19:56:43.320:INFO::main: Logging initialized @2009ms to org.eclipse.jetty.util.log.StdErrLog

2023-05-24 19:56:43.466:INFO:oejs.Server:main: jetty-9.4.9.v20180320; built: 2018-03-20T12:21:10Z; git: 1f8159b1e4a42d3f79997021ea1609f2fbac6de5; jvm 17.0.5+8-nixos

2023-05-24 19:56:43.705:INFO:oejs.Server:main: Started @2402ms
Bbrody5/24/2023
is maven listening on host 0.0.0.0 and the environment variable PORT
OOliver_5/24/2023
This would be our maven POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>groupId</groupId>
<artifactId>backend</artifactId>
<version>1.0-SNAPSHOT</version>



<build>



<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>copy</goal></goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-runner</artifactId>
<version>9.4.9.v20180320</version>
<destFileName>jetty-runner.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.4.44.v20210927</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-server</artifactId>
<version>9.4.44.v20210927</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-servlet</artifactId>
<version>9.4.44.v20210927</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
</dependencies>



</project>
OOliver_5/24/2023
Btw I are really sorry I come across as stupid, but I am very new to hosting services and websockets
Bbrody5/24/2023
^
OOliver_5/24/2023
How would I look for if maven i listening for 0.0.0.0 and the variable port?
Bbrody5/24/2023
PORT***
Bbrody5/24/2023
i wouldnt know, ive never touched java in my life, i just know once you get maven to listen on host 0.0.0.0 and environment variable PORT it should work
Bbrody5/24/2023
so, read the documentation for maven? stack overflow, etc
OOliver_5/24/2023
Alright no worries! Last dumb question then, is the environment variable PORT in railway and what you recommend having it as?
OOliver_5/24/2023
Im guessing it should look something like this but with a acutall port in it:
Bbrody5/24/2023
you shoulnt be setting the PORT yourself if you can avoid it, and especially not in shared variables
Bbrody5/24/2023
the environment variable PORT is one railway generates for you, no need to define it yourself
Aangelo5/24/2023
Yep this is likely it
OOliver_5/24/2023
Copy that!
OOliver_5/24/2023
This what I came up with when using maven:

<httpConnector>
<host>0.0.0.0</host>
<port>${PORT}</port>
</httpConnector>
OOliver_5/24/2023
Still trying to get the environment variable PORT to work
Bbrody5/24/2023
find out how you can get maven to grab the PORT variable from the enviroment