Private networking with Railway generated ports

Hey Folks, Sorry if crossposting this is a big no-no, figured maybe Discord is easier for conversational things. I raised https://community.railway.app/discuss/thread/how-do-i-use-private-networking-with-rai-65fa14c1 but TL;DR: How do I use private networking (internal hostnames) when I let Railway assign ports?
Railway Community Forum
How do I use private networking with railway generated ports
Hey Folks, As mentioned in the documentation, it's recommended to stick to the ports railway sets on your services, coolio. When I want to make a request to a service on the internal network however, how can I know the port that Railway has assigned it? Looking at: https://docs.railway.app/guides/private-networking#use-internal-hostname-and-p...
71 Replies
Percy
Percy10mo ago
Project ID: cb5aa03d-c72c-426c-8dd7-f109b7f03b5d
CiaranSweet
CiaranSweet10mo ago
cb5aa03d-c72c-426c-8dd7-f109b7f03b5d
Brody
Brody10mo ago
there's a wonderful guide here https://docs.railway.app/guides/private-networking but yes it's good your apps are listening on the automatically assigned PORT variable, so that means getting your apps to listen on a static port for use with the private network is as simple as setting a PORT service variable
CiaranSweet
CiaranSweet10mo ago
Yeah, found the guide, guessed it was the case that I would have to define a port instead 😄 No worries.
Brody
Brody10mo ago
what kind of frontend is this?
CiaranSweet
CiaranSweet10mo ago
Sveltekit
Brody
Brody10mo ago
server side rendered?
CiaranSweet
CiaranSweet10mo ago
🤷‍♂️ I'm a frontend noob, much more comfortable kneedeep in AWS infra and Python 😄 I manage a bunch of Svelte devs so figured I should learn it a bit haha
Brody
Brody10mo ago
will the calls to the backend be made from the server side of your frontend app?
CiaranSweet
CiaranSweet10mo ago
Yes, it already is, you can't see the requests in devtools for example
Brody
Brody10mo ago
perfect then you will want a service variable on the frontend service like this
BACKEND_URL=http://${{<backend service name>.RAILWAY_PRIVATE_DOMAIN}}:${{<backend service name>.PORT}}
BACKEND_URL=http://${{<backend service name>.RAILWAY_PRIVATE_DOMAIN}}:${{<backend service name>.PORT}}
then use that variable in your frontend's code when you make the fetch call
CiaranSweet
CiaranSweet10mo ago
Ohlala. Don't suppose there's a page with all these magic env vars?
CiaranSweet
CiaranSweet10mo ago
Sweet. That snippet you sent would be super useful on the Private Networking guide, makes much more sense now 😄 Annoyingly I don't seem to have the Solution App available
Brody
Brody10mo ago
I shall make that happen
CiaranSweet
CiaranSweet10mo ago
I have another question on the community page, feel bad duplicating it here also 🥲 Buuuuut if you're around 👀
Brody
Brody10mo ago
solution app?
CiaranSweet
CiaranSweet10mo ago
No description
CiaranSweet
CiaranSweet10mo ago
No description
Brody
Brody10mo ago
I turned that off because I had too many people marking the complete wrong message as the answer
CiaranSweet
CiaranSweet10mo ago
Ah 😛
Brody
Brody10mo ago
but I haven't turned off the message lol
CiaranSweet
CiaranSweet10mo ago
No worries haha
Brody
Brody10mo ago
I'll be honest I'm happy you posted here too, and I'd prefer you do, because as you said discord is easier for conversational things. the forums is lacking a conversational aspect and that's why I don't like using them, but the cheerleader for the forums knows that too!
CiaranSweet
CiaranSweet10mo ago
Righto. I'll make a dupe here then 😅
Brody
Brody10mo ago
sounds good!
CiaranSweet
CiaranSweet10mo ago
Back on this one 😅 So I put:
BACKEND_URL=http://${{backend.RAILWAY_PRIVATE_DOMAIN}}:${{backend.PORT}}
BACKEND_URL=http://${{backend.RAILWAY_PRIVATE_DOMAIN}}:${{backend.PORT}}
This resolves but if I put Https or http, it fails to connect, the more concerning part is the PORT resolves to not what the backend service is actually listening on:
errno: -111,

code: 'ECONNREFUSED',

syscall: 'connect',

address: 'fd12:2030:e370::8f:bc37:5946',
errno: -111,

code: 'ECONNREFUSED',

syscall: 'connect',

address: 'fd12:2030:e370::8f:bc37:5946',
Backend logs:
[INFO] Running on http://[::]:6642
[INFO] Running on http://[::]:6642
Am I using the right variables? Hmmm okay, in the dashboard it resolves ${{backend.RAILWAY_PRIVATE_DOMAIN}} as backend.railway.internal however when that's injected into my service, it's actually the ipv6 address.
CiaranSweet
CiaranSweet10mo ago
If I then use [ ] for fetch like recommended for ipv6 addresses, it resolves to:
No description
CiaranSweet
CiaranSweet10mo ago
Safe to say I'm scuppered 😛
BACKEND_URL=http://[${{backend.RAILWAY_PRIVATE_DOMAIN}}]:${{backend.PORT}}
BACKEND_URL=http://[${{backend.RAILWAY_PRIVATE_DOMAIN}}]:${{backend.PORT}}
This is what I have right now, Railway setup the port for backend, so I assume I'll be able to access that value. This resolves to 'http://[backend.railway.internal]:' If I remove the [] from my ENV (Even though that's not what I should do), it resolves to the actual ipv6 address. Either way I do it, PORT Is empty anyway 🥲 Okay, so a little progress:
BACKEND_URL=http://${{backend.RAILWAY_PRIVATE_DOMAIN}}:6642
BACKEND_URL=http://${{backend.RAILWAY_PRIVATE_DOMAIN}}:6642
If I hardcode the port to what I see in my backends logs (from the port Railway generated), I get a connection So I guess the real question is - How the heck do I get that port without log diving 😄 (I'd rather not pin it as it just another bit of config to remember)
Brody
Brody10mo ago
pinning the port by setting a PORT service variable is perfectly fine as long as your app is still listening on the PORT environment variable, and that's exactly what you need to do to get that reference variable to render correctly. it's also absolutely necessary because without pinning the port, railway will assign a random port (via the PORT environment variable) on every deployment that it expects your app to listen on, you can't reference this magic variable and even of you could it would just makes things worse because its random also I have never seen the square brackets be necessary, so try not using the brackets
CiaranSweet
CiaranSweet10mo ago
Yeah, it's just strange that if I set the value, I can refer to it, but if Railway does, I can't 😂 Alright, I shall just pin it! Is that something I can define in railway.json? Or dashboard only?
Brody
Brody10mo ago
yeah I get ya, but it's a good thing you can't reference the automatically generated PORT, as the value would become stale very quickly anyway, it's random and only available to the deployment at runtime. has to be defined as a service variable in the dashboard
CiaranSweet
CiaranSweet10mo ago
Yeah that's fair enough, just somewhat invalidates the 'magic' 😂 Anyway, I'll switch to that, thanks once again Brody! You the MVP
Felipe Cardoso
Felipe Cardoso7mo ago
Hi folks, I'm having trouble using private networks I read all the content above, including the links, followed the tutorial in the last link sent above, but I was unsuccessful. I have two backend services running on railway, where currently one communicates with the other via the public network. I would like to leave this second service with the public route disabled, and use the private network to communicate with it. The two services have the same project name and environment, as I understood from the document above. The service that will receive requests via the private route is configured to listen on IPv6. I tested calling the url numerous times using ###.railway.internal and port, but it always returns "getaddrinfo ENOTFOUND" I don't know what else I can try, does anyone suggest anything? Both projects are in nestJs with typescript.
Brody
Brody7mo ago
nixpacks or dockerfile based deployments?
Felipe Cardoso
Felipe Cardoso7mo ago
Nixpacks
Brody
Brody7mo ago
at what point in your apps lifecycle are you trying to call the other service?
Felipe Cardoso
Felipe Cardoso7mo ago
The first service, with public routes, is an API gateway that, depending on the requested route, makes a call to this second service. This call I would like to make via private route internally.
Brody
Brody7mo ago
does your service make a call to the other service within the first 3 seconds of itself starting?
Felipe Cardoso
Felipe Cardoso7mo ago
no, never
Brody
Brody7mo ago
screenshot of your project please
Felipe Cardoso
Felipe Cardoso7mo ago
what do you want i screen shot exactly?
Brody
Brody7mo ago
of your railway project
Felipe Cardoso
Felipe Cardoso7mo ago
the second service
No description
Brody
Brody7mo ago
where is the other service
Felipe Cardoso
Felipe Cardoso7mo ago
the first, that uses the second
No description
Brody
Brody7mo ago
these are separate projects
Felipe Cardoso
Felipe Cardoso7mo ago
they are different reppositories is there any how deploy toggether? ok, i got it i just saw the "new" button
Brody
Brody7mo ago
you can't communicate to services in different projects
Felipe Cardoso
Felipe Cardoso7mo ago
No description
Brody
Brody7mo ago
wouldn't be very private if you could
Felipe Cardoso
Felipe Cardoso7mo ago
yeah, i undertand it i thought as i set the project name as the same, it would work as one
Brody
Brody7mo ago
gotcha
Felipe Cardoso
Felipe Cardoso7mo ago
i use railway for almost 2 years the wrong way 🥲 I'd never seen that "new" button i have a lot to do now.. re-managing the services of my projects
Brody
Brody7mo ago
yeah you aren't the first and until railway stops allowing users to deploy services from the dashboard, you will not be the last
Felipe Cardoso
Felipe Cardoso7mo ago
now it makes more sense why it has to be in the same project AND enviroment
Brody
Brody7mo ago
indeed
Felipe Cardoso
Felipe Cardoso7mo ago
thank you its already working
Brody
Brody7mo ago
awesome
Santiago Baron
Santiago Baron2mo ago
Hello @Brody , I have a question I want my front to connect to my backend using Private Network, but I haven't been able to do it... I checked this post but nothing helps... any ideas? I have this as follows:
No description
Santiago Baron
Santiago Baron2mo ago
And my code is this:
export const getStrapiURL = () => {

if (process.env.NODE_ENV === 'production') {
return 'https://espectrosoft-backend.railway.internal'
}

if (process.env.NODE_ENV === 'development') {
return 'http://localhost:1337';
}
}
export const getStrapiURL = () => {

if (process.env.NODE_ENV === 'production') {
return 'https://espectrosoft-backend.railway.internal'
}

if (process.env.NODE_ENV === 'development') {
return 'http://localhost:1337';
}
}
Brody
Brody2mo ago
a client side rendered frontend app can not use the internal domain of the backend, you need to use the public domain. it wouldnt be a very private network if anyone's browser could make requests to it.
Santiago Baron
Santiago Baron2mo ago
I'm not exactly using client side, I'm doing it with Nextjs and I make these requests using server side
Santiago Baron
Santiago Baron2mo ago
I tried to do that but in my variables I can't find the port
Brody
Brody2mo ago
you need to set it then
Santiago Baron
Santiago Baron2mo ago
No description
Santiago Baron
Santiago Baron2mo ago
And it can be any port or is there any guide regarding that?
Brody
Brody2mo ago
8080 is a good port make sure your app is listening on the $PORT environment variable too
Santiago Baron
Santiago Baron2mo ago
Got it, I'll try it
Want results from more Discord servers?
Add your server