CodexBloom - Programming Q&A Platform

OCI Load Balancer Timeout implementing WebSocket Connections in Node.js Application

👀 Views: 79 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-09
oci websocket load-balancer node.js JavaScript

I'm converting an old project and I've been working on this all day and I'm experiencing a frustrating scenario with my Node.js application deployed on Oracle Cloud Infrastructure (OCI) where the WebSocket connections are timing out unexpectedly. I'm using the OCI Load Balancer to route traffic to my application, which is hosted on a Compute instance. The load balancer is set up with a backend set that has a health check configured, but it seems to interfere with long-lived WebSocket connections. I've configured the load balancer with the following settings: - Backend set protocol: HTTP - Backend health check interval: 10 seconds - Backend timeout: 30 seconds However, after about 30 seconds of inactivity, the WebSocket connection gets dropped, and I see the following behavior in the browser console: ``` WebSocket connection to 'wss://example.com/socket' failed: behavior during WebSocket handshake: Unexpected response code: 502 ``` In my Node.js application, I'm using the `ws` library to manage WebSocket connections: ```javascript const WebSocket = require('ws'); const socket = new WebSocket('wss://example.com/socket'); socket.on('open', function () { console.log('Connected to WebSocket'); }); socket.on('close', function () { console.log('WebSocket connection closed'); }); socket.on('behavior', function (behavior) { console.behavior('WebSocket behavior:', behavior); }); ``` To mitigate this timeout scenario, I tried increasing the backend timeout to 60 seconds, but it did not resolve the question. I also checked the OCI Load Balancer logs and noticed intermittent 502 Bad Gateway errors around the time the WebSocket connections drop. Has anyone encountered similar issues with OCI Load Balancers and WebSocket connections? Any suggestions on how to prevent these timeouts or a better way to configure the load balancer for long-lived connections? Thanks in advance! My development environment is Linux. Am I missing something obvious? Is there a better approach? Any pointers in the right direction?