Reflex App WebSocket Connection Fails on Railway - "WebSocket is closed before connection..."
michael-lyon
FREEOP

8 months ago

Hey Railway team! 👋

I'm having persistent WebSocket connection issues with my Reflex (Python web framework) app deployed

on Railway. The frontend loads perfectly, but WebSocket connections fail immediately.

Error Pattern:

  • ✅ Frontend loads correctly
  • ❌ WebSocket connection to wss://my-app.up.railway.app/_event fails
  • ❌ Browser shows "101 Switching Protocols" response followed immediately by connection close
  • ❌ Error: "WebSocket is closed before the connection is established"

What I've Tried:

  1. Single PORT approach: Backend listening on $PORT environment variable
  2. Backend-only deployment: Serving static frontend from backend on same port
  3. Header simplification: Removed potentially problematic proxy headers
  4. Debug logging: Enabled --loglevel debug but no websocket-specific errors shown

Current Configuration:

nixpacks.toml

[start]

cmd = 'reflex run --backend-only --env prod --backend-host 0.0.0.0 --backend-port $PORT'

Key Details:

  • Backend starts successfully on Railway's PORT
  • /ping endpoint works fine (returns pong)
  • WebSocket server should be accessible at /_event
  • Same codebase works locally with websockets
  • Using Reflex 0.8.11 (Python web framework with Socket.IO-based websockets)

Questions:

  1. Are there any Railway-specific WebSocket limitations I should know about?
  2. Do I need special configuration for WebSocket upgrades?
  3. Any suggestions for debugging WebSocket connection issues on Railway?
$10 Bounty

19 Replies

irazvan2745
FREE

8 months ago

are you sure its railway and not your app?


michael-lyon
FREEOP

8 months ago

i believe i have everything set up properly on my app.

If you have any suggestions, i'd love to hear.


irazvan2745
FREE

8 months ago

is localhost working with no issues?


michael-lyon
FREEOP

8 months ago

yes


irazvan2745
FREE

8 months ago

try with railpack


irazvan2745
FREE

8 months ago

nixpacks is kinda deprecated


irazvan2745
FREE

8 months ago

small chances that it will make a difference, but worth a shot


michael-lyon
FREEOP

8 months ago

let me try to do that.


michael-lyon
FREEOP

8 months ago

is it possible you share your sample with me?


irazvan2745
FREE

8 months ago

?


michael-lyon
FREEOP

8 months ago

railpack


irazvan2745
FREE

8 months ago

just


irazvan2745
FREE

8 months ago

configure it through the ui?


michael-lyon
FREEOP

8 months ago

how do you do that?


irazvan2745
FREE

8 months ago

railway ui?


coding-dynasty
HOBBY

8 months ago

I think your server lacks the upgrade header needed by websockets to work.



coding-dynasty
HOBBY

8 months ago

you need to proxy your server with one of these (nginx,caddy,traefik) so to alter the headers.


dosp0911
HOBBY

6 months ago

Have you solved this issue?


Welcome!

Sign in to your Railway account to join the conversation.

Loading...