Tuning for Server Sent Events and WebSockets
on Passenger + Apache
Using SSE with WebSockets generally means that an application must handle multiple concurrent connections. Ruby applications usually can't do this and need a special configuration tweak.
Ruby concurrency tweak
Passenger's highly optimized load balancer assumes that Ruby apps can handle 1 (or thread-limited amount of) concurrent connection(s). This is usually the case and results in optimal load-balancing. But endpoints that deal with SSE/Websockets can handle many more concurrent connections, so the assumption leads to degraded performance.
You can use the force max concurrent requests per process configuration option to override this. The example below shows how to set the concurrency to unlimited for /special_websocket_endpoint:
<VirtualHost *:80> ServerName www.example.com DocumentRoot /webapps/my_app/public # Use default concurrency for the app. But for the endpoint # /special_websocket_endpoint, force a different concurrency. <Location /special_websocket_endpoint> PassengerAppGroupName foo_websocket PassengerForceMaxConcurrentRequestsPerProcess 0 </Location> </VirtualHost>
config.ruto set the concurrency (on the entire app).
if defined?(PhusionPassenger) PhusionPassenger.advertised_concurrency_level = 0 end
Action Cable integration
Are you using Ruby on Rails with Action Cable? Please refer to our Action Cable integration guide.
You can find Passenger SSE and WebSocket demo apps on the Passenger documentation overview page, under section "Demos".