Websockets
ahmedmajidgit
PROOP

10 months ago

I have an application using Node.js and Socket.io, but it's much slower compared to Heroku, which has affected the real-time communication performance. In contrast, my other app, hosted on Railway with Node.js and Express.js, has significantly faster API responses than on Heroku.

Could you explain the issue with WebSockets and how to overcome this?

$20 Bounty

2 Replies

10 months ago

This thread has been marked as public for community involvement, as it does not contain any sensitive or personal information. Any further activity in this thread will be visible to everyone.

Status changed to Open brody • 10 months ago


domehane
FREE

10 months ago

there's actually some railway-specific stuff going on here that's probably causing your socket.io slowness.

first thing, if you're running multiple replicas on railway that's likely your main problem. railway doesn't support sticky sessions for load balancing, so your websocket connections might be getting bounced between different instances which totally breaks socket.io's stateful connections. your express app doesn't have this issue because regular http requests don't care which instance handles them.

try thos:

-- scale down to a single replica if you're using multiple. i know it sucks but railway just doesn't handle websocket load balancing well right now. if you really need multiple i instances you'll have to set up your own load balancing with redis or something

-- make sure your websockets are actually connecting properly. open browser dev tools and check the network tab - you should see a websocket connection upgrade, not a bunch of polling requests. if it's falling back to long-polling that's where your slowness is coming from

-- set keepalive pings to around 20-25 seconds. railway can have some tcp idle timeout issues and other users recommend sending traffic every 10-30 seconds to keep connections alive

-- double check your cors settings allow the upgrade header and your origins are configured right. sometimes websockets fail the upgrade silently and fall back to polling

-- make sure you're binding to process.env.PORT and 0.0.0.0 since railway uses dynamic ports

-- i want to add another point but i think this is largely enough :)

once you get the websocket connections stable and running on a single replica, railway's infrastructure should handle the real-time communication much better. the platform works great, just needs the right config for websockets


domehane

there's actually some railway-specific stuff going on here that's probably causing your [socket.io](http://socket.io) slowness. first thing, if you're running multiple replicas on railway that's likely your main problem. railway doesn't support sticky sessions for load balancing, so your websocket connections might be getting bounced between different instances which totally breaks [socket.io](http://socket.io)'s stateful connections. your express app doesn't have this issue because regular http requests don't care which instance handles them. try thos: \-- scale down to a single replica if you're using multiple. i know it sucks but railway just doesn't handle websocket load balancing well right now. if you really need multiple i instances you'll have to set up your own load balancing with redis or something \-- make sure your websockets are actually connecting properly. open browser dev tools and check the network tab - you should see a websocket connection upgrade, not a bunch of polling requests. if it's falling back to long-polling that's where your slowness is coming from \-- set keepalive pings to around 20-25 seconds. railway can have some tcp idle timeout issues and other users recommend sending traffic every 10-30 seconds to keep connections alive \-- double check your cors settings allow the upgrade header and your origins are configured right. sometimes websockets fail the upgrade silently and fall back to polling \-- make sure you're binding to process.env.PORT and 0.0.0.0 since railway uses dynamic ports \-- i want to add another point but i think this is largely enough :) once you get the websocket connections stable and running on a single replica, railway's infrastructure should handle the real-time communication much better. the platform works great, just needs the right config for websockets

ahmedmajidgit
PROOP

10 months ago

I have a Socket.IO server and two HTTPS servers. In the Socket server, I maintain two environments (production and test). As far as I understand, having multiple environments does not count as running multiple replicas, so I don’t think that’s the cause of the slowness.

I’ll try the points you mentioned. I’d also be interested in hearing the other point you were going to add.


Welcome!

Sign in to your Railway account to join the conversation.

Loading...