Private networking in SvelteKit app
I've deployed a SvelteKit application on Railway using a Dockerfile. Within that project I have a separate service for cron jobs, also deployed with a Dockerfile.
The cron service calls endpoints on my SvelteKit application. This works fine using the public domain of the app service using HTTPS, but when attempting to use private networking it doesn't work.
In my app service, I've configured the server to listen on
0.0.0.0
with Railway's injected port. This builds well and logs Listening on 0.0.0.0:8080
, so I know it's working.
26 Replies
Project ID:
95e92fe0-66e9-4eed-86df-2cdc05218e79
95e92fe0-66e9-4eed-86df-2cdc05218e79
Here's my cron-service.ts, which also deploys with no problems.
Unfortunately, my logs show a connection error
Error executing test cron job: request to http://sveltecareers.railway.internal:8080/api/cron-tester failed, reason: connect ECONNREFUSED fd12:3663:d458::e6:bba7:55ec:8080
. Any idea why this is happening? I believe the ports line up and I'm making a HTTP request.
A collorary question is how to access the ${{sveltecareers.PORT}}
variable as described in https://docs.railway.app/guides/private-networking#use-internal-hostname-and-port. I cannot access the main app service's PORT variable from the cron-schedule service. I've worked around that by referencing process.env.PORT
directly in the cron-service.ts
assuming that the injected PORT would be the same, which it appears to be (8080
).
Thanks! Loving the platform so far.the injected port would be the same, but its never good to assume that, you would want to set a PORT service variable on the SvelteKit service so that you can reference it on the cron service.
as for the
ECONNREFUSED
error, remove the server
config, and then simply set a HOST
service variable to ::
since the private network is IPv6 only.Thanks for the quick response @Brody!
When setting the PORT service variable on the SvelteKit service, is there any way to retrieve this as a Railway provided variable? Or is PORT only made available during runtime, but not in the GUI? I wasn't able to select it from the dropdown in the service variables menu. Do I have to set a fixed port, like
8080
now that I know that's the injected one?
I don't understand what you mean with setting a HOST
service. Do you mean to remove the server
property I had defined from the vite.config.ts
altogether?yeah you are right, its only available during runtime, you can just set a fixed port in the UI though.
Ah sorry I think I'm grokking now. You're suggesting I set a
HOST
service variable. On the SvelteKit Railway service or the cron service?yes, service variable, on the SvelteKit service
and also remove the server property in the config file, its not needed
OK so summarizing:
On the SvelteKit service I remove the
service
prop from the vite.config.ts
. I set a fixed PORT
service variable to 8080
instead, and a HOST
service variable to ::
. This allows me to retrieve these on the cron service, where I define the full private origin as: http://${{sveltecareers.RAILWAY_PRIVATE_DOMAIN}}${{sveltecareers.HOST}}${{sveltecareers.PORT}}
. Is that right?you got most correct.
the url should be made like this
http://${{sveltecareers.RAILWAY_PRIVATE_DOMAIN}}:${{sveltecareers.PORT}}
the HOST
variable is just set so that the server knows to listen on IPv6 is allAh I see. But I don't need to specify this in vite for the SvelteKit build, because Railway will inject this during run time?
Do I not use double colons in the private origin? Just single colon?
yeah the SvelteKit server will look for and use a
HOST
variable(First time doing private networking on any hosting service and I'm new to IP standards like IPv6, so bear with me 😅)
not sure what you mean, simply set
HOST=::
in the SvelteKit service, no need to overthink this right nowFunny, today I learned that
https://
always defaults to port 443
. And that explains why I need to define a port when using http://
.
OK got it, thanks Brody. Will work on implementing this r/n.set
HOST=::
and PORT=8080
on sveltecareers
set MAIN_APP_PRIVATE_DOMAIN="http://${{sveltecareers.RAILWAY_PRIVATE_DOMAIN}}:${{sveltecareers.PORT}}"
on cron-service
and then simply use MAIN_APP_PRIVATE_DOMAIN
when you call sveltecareers
🫡
This worked, thanks for helping me fix.
It may be worthwhile updating the private networking docs to clarify that while the PORT variable is automatically injected by Railway during runtime, it's not accessible as a reference variable and needs to be set manually.
Unrelated question -- the SvelteKit and the cron scheduler are both in a monorepo. I'm deploying based on Github commits. Is there any way to only deploy one service when a particular file or folder in my repo changes? Right now when I change the cron service, the whole base app re-deploys.
its not specific to private networking, i think its mentioned elsewhere in the docs though.
you want watch paths - https://docs.railway.app/guides/monorepo#watch-paths
Oooh that's sweet, will try now.
Works like a charm, thanks again!
Tagging this as solved.
no problem!
After implementing your feedback, this branch passed my CI/CD checks and I merged onto my main branch. I then updated the branch to be watched to main in my services.
However since redeploying, they are both showing strange deploy logs. The cron scheduler runs into an import issue:
It pertains to this import, which should work fine. I've confirmed the path holds true for the
dist
folder which tsconfig.ts
is successfully configured to build into.
Any idea what could be going wrong? The exact same codebase worked when I had it deployed via Github onto Railway in my previous branch.I'm sorry but we wouldn't be able to provide coding help here
perhaps you have the import named wrong or the file named incorrectly, but beyond this we can't help here, these forums are for platform help
Sure I understand. I'm just trying to ascertain why this deployment would have worked well when on a different branch, but failed now that I merged it. I must have made a mistake somewhere, but struggle to determine what it is.
really sorry but we can't offer support for application level issues
Understood I'll try resolve in the SvelteKit Discord.
Thanks for checking.
sounds good