This guide explains how to configure WebSockets for the Beton-AI application on Railway.
The backend WebSocket server is already configured in backend/src/services/websocketService.ts and initialized in backend/src/index.ts.
Key features:
- WebSocket server runs on the same port as Express (3001)
- WebSocket path:
/ws - Supports real-time job progress updates
- Automatic client reconnection with exponential backoff
The backend CORS is configured to allow WebSocket connections from:
- Development:
http://localhost:3000 - Railway production:
https://*.up.railway.app
Set these environment variables in your Railway frontend service:
NEXT_PUBLIC_WS_URL=wss://your-backend-service-name.up.railway.app/wsImportant: Use wss:// (WebSocket Secure) for Railway production, not ws://.
The frontend automatically detects the environment:
- If
NEXT_PUBLIC_API_URLcontainsrailway.app, it convertsws://towss:// - Falls back to
ws://localhost:3001/wsfor development
- Ensure your backend Railway service is deployed and healthy
- Note your backend service URL (e.g.,
https://beton-ai-backend-production.up.railway.app)
- Add the WebSocket environment variable:
NEXT_PUBLIC_WS_URL=wss://beton-ai-backend-production.up.railway.app/ws
- Redeploy the frontend service
- Open browser dev tools → Network tab
- Navigate to your frontend application
- Start a bulk download job
- Look for WebSocket connection in Network tab:
- Should show
101 Switching Protocolsstatus - Connection type:
websocket - URL:
wss://your-backend-domain/ws
- Should show
-
WebSocket connection fails with 403/404
- Check that the backend service is deployed and healthy
- Verify the WebSocket URL format:
wss://backend-domain/ws
-
Connection established but no messages
- Check backend logs for WebSocket authentication
- Verify user ID is being passed correctly
- Check that jobs are actually running (Redis connection working)
-
Frequent disconnections
- Railway may terminate idle connections
- Our implementation includes automatic reconnection
- Check connection timeout settings
You can test the WebSocket connection manually:
// In browser console
const ws = new WebSocket('wss://your-backend-domain/ws');
ws.onopen = () => {
console.log('Connected');
ws.send(JSON.stringify({type: 'auth', userId: 'test-user-id'}));
};
ws.onmessage = (event) => {
console.log('Message:', JSON.parse(event.data));
};-
Bulk Download Progress
- Real-time job progress updates
- Job completion notifications
- Job failure alerts
-
CSV Upload Progress
- Upload progress tracking
- Processing status updates
-
Connection Management
- Automatic reconnection
- Multiple client support per user
- Heartbeat/ping-pong for connection health
- Railway supports WebSockets natively
- No special load balancer configuration needed
- WebSocket connections are sticky to specific instances
- Consider horizontal scaling implications for multi-instance deployments