R
Railway9mo ago
Roy

Prebuilt Mqtt Service and Ports

Looking for some guidance. Currently have a couple different services in my project, including a postgres db, mqtt broker, a publisher and a subscriber. The mqtt broker service uses an officially hosted docker image. That service internally opens port 1883 for http mqtt communication, 8080 for ws mqtt communication, as well as other ports for misc things like 8888 has routes /health for health check and /status for a management dashboard. Right now only 8080 is exposed via Railways PORT env var and works perfectly for my web socket connections on other services. Since I can only expose one port via railway does that mean it’s impossible to take advantage of those other pieces? I’m open to any sort of strategy, but don’t know if I could handle modifying the source of the broker itself so would prefer a different option than that Thanks!
17 Replies
Percy
Percy9mo ago
Project ID: 40a3bc08-0c66-4f5e-a268-736ec53f0cac
Roy
Roy9mo ago
40a3bc08-0c66-4f5e-a268-736ec53f0cac
Brody
Brody9mo ago
youd need to run a proxy service to map routes to ports on the mqtt service. this proxy service would be exposed publicly, and proxy incoming requests to the mqtt service via the private network, example: mqtt-proxy.up.railway.app -> mqtt.railway.internal:1883 mqtt-proxy.up.railway.app/ws -> mqtt.railway.internal:8080 mqtt-proxy.up.railway.app/misc-> mqtt.railway.internal:8888
Roy
Roy9mo ago
Ohh I see, so the internal ports are still available to other services on the network?
Brody
Brody9mo ago
yep, its a private/local network after all
Roy
Roy9mo ago
So I have an express api also running in my project. If I add a route say /mqtt/health. I can make an internal request to the appropriate port and just forward that response?
Brody
Brody9mo ago
yes, but you really should use a proper proxy service like caddy or nginx if thats more your thing, but i think caddy is far more approachable
Roy
Roy9mo ago
Gotcha thanks for helping me out so quickly. Feel kinda dumb needing to ask that question… lol
Brody
Brody9mo ago
no worries at all
Roy
Roy9mo ago
proxy is pretty easy to see since it’s a private local network I think I’ll checkout caddy, though nginx is something I do have experience with using before
Brody
Brody9mo ago
caddy is just simpler imo, ive done this with both nginx and caddy, and caddy was the clear winner purely on ease of use and ease of configuration, example https://github.com/brody192/reverse-proxy/blob/main/Caddyfile
Roy
Roy9mo ago
Gonna check it out right after I finish eating lunch and try deploying it - really appreciate the guidance!
Brody
Brody9mo ago
no problem! if you need help with caddy let me know
Roy
Roy9mo ago
I've got to make a choice. I have an custom domain, my-project.app and right now have railway connected on two difference services to two different subdomains, specifically, mqtt.my-project.app and api.my-project.app. mqtt is going to have a reverse proxy to cover things like / - http, /ws - ws, /health - health check, /admin dashboard, and the api contains the other business logic (mainly uses trpc mounted at /trpc), but will also expose public graphql endpoints for a different piece. Do you think I'd be doing too much by having a caddy proxy in front of both so that there can continue to be a separation of concerns. Or would it be better to remove the mqtt sub domain entirely and just use one proxy to route all the relevant mqtt requests using a /mqtt/... prefix?
Brody
Brody9mo ago
I don't see why you'd want to put a proxy Infront of your api. remove any and all public domains from the mqtt service and move them over to the proxy service
Roy
Roy9mo ago
I like that option, it really doesn't need to be in front of the api. So I'll have caddy at mqtt.my-project.app to handle proxying the request to different ports on the mqtt service and I'll keep my standalone api service unaffected. Thanks again for the direction
Brody
Brody9mo ago
exactly! id love to hear about the outcome of this!