Railway reflex template websocket connection error
dosp0911
HOBBYOP

5 months ago

caddy file:

{
	admin off
	persist_config off
	auto_https off

	log {
		format json
	}

	servers {
		trusted_proxies static private_ranges
	}
}

:{$PORT} {
	log {
		format json
	}

	# ----------------------
	# Backend (API / WS)
	# ----------------------
	@backend {
		path /backend/*
	}

	@backend_ws {
		path /backend/*
		header Connection *Upgrade*
		header Upgrade websocket
	}

	handle_path @backend_ws {
		reverse_proxy 127.0.0.1:8000
	}

	handle_path @backend {
		reverse_proxy 127.0.0.1:8000
	}

	# ----------------------
	# Static Frontend
	# ----------------------
	root * .web/_static
	encode gzip
	try_files {path} {path}.html
	file_server

	# ----------------------
	# Errors
	# ----------------------
	handle_errors {
		rewrite * /{err.status_code}.html
		file_server
	}
}

rx.config

config = rx.Config(
    app_name="web",
    title="title",
    description="desc",
    backend_port=8000,
    frontend_port=3000,
    db_url=DATABASE_URL,
    cors_allowed_origins=[
        "http://127.0.0.1:3000",
        f"https://{RAILWAY_PUBLIC_DOMAIN}:3000",
        f"http://127.0.0.1:8080",
        f"https://{RAILWAY_PUBLIC_DOMAIN}:8080",
    ],
    api_url=(
        f"https://{RAILWAY_PUBLIC_DOMAIN}/backend"
        if RAILWAY_PUBLIC_DOMAIN  
        else "http://127.0.0.1:8000"
    )
)

It still happens wss connection error. I cannot find any solutions.
I have tried ever things that people suggest in railway-station discussion.

Check if server is reachable at wss://reflex-production-6b92.up.railway.app/backend/_event

$10 Bounty

1 Replies

domehane
FREE

5 months ago

the prblm is handle_path strips /backend before forwarding to reflex. change it to just handle instead:

caddy

handle @backend_ws {
    reverse_proxy 127.0.0.1:8000
}

handle @backend {
    reverse_proxy 127.0.0.1:8000
}

also remove the ports from your cors origins in rx.config - railway doesn't use :3000 or :8080 in public urls:

python

cors_allowed_origins=[
    "http://127.0.0.1:3000",
    f"https://{RAILWAY_PUBLIC_DOMAIN}",
],

this should fix your wss connection to /backend/_event


Welcome!

Sign in to your Railway account to join the conversation.

Loading...