WebSockets with Railway
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.
22 Replies
Project ID:
e929f490-a8af-4200-97d4-e6e331581ff3
e929f490-a8af-4200-97d4-e6e331581ff3
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
Hi Brody, We will try it
Should there still be wss://0.0.0.0 ?
wss://<whatever your railway domain is>/<whatever your websocket endpoint is>
So it would be wss://backend-production-6ad2.up.railway.app/
The domain would be from our backend (java) Railway app
if the root endpoint
/
handles websocket connections and not something like /ws
then yes that is correctFor the record, we recommend adding a custom domain for websockets
Railway proxy does terminate sessions after 30 seconds for scale reasons on *.up.railway.app domains
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.
Going to need some logs my guy
We have a custom domain for the future
Sure thing!
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
is maven listening on host 0.0.0.0 and the environment variable PORT
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> Btw I are really sorry I come across as stupid, but I am very new to hosting services and websockets
<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> Btw I are really sorry I come across as stupid, but I am very new to hosting services and websockets
^
How would I look for if maven i listening for 0.0.0.0 and the variable port?
PORT***
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
so, read the documentation for maven? stack overflow, etc
Alright no worries! Last dumb question then, is the environment variable PORT in railway and what you recommend having it as?
Im guessing it should look something like this but with a acutall port in it:
you shoulnt be setting the PORT yourself if you can avoid it, and especially not in shared variables
the environment variable PORT is one railway generates for you, no need to define it yourself
Yep this is likely it
Copy that!
This what I came up with when using maven:
<httpConnector>
<host>0.0.0.0</host>
<port>${PORT}</port>
</httpConnector>
Still trying to get the environment variable PORT to work
find out how you can get maven to grab the PORT variable from the enviroment