CodexBloom - Programming Q&A Platform

OCI Load Balancer Configuration for WebSocket Connections in a Node.js Application

👀 Views: 39 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-11
OCI WebSocket Node.js JavaScript

I've been struggling with this for a few days now and could really use some help... I'm currently working on a Node.js application hosted on Oracle Cloud Infrastructure (OCI) that utilizes WebSocket connections for real-time communication. I've set up an OCI Load Balancer to distribute traffic among multiple instances of my Node.js application, but I'm running into issues with maintaining the WebSocket connections. Specifically, clients frequently experience disconnections after a few minutes of inactivity, and I suspect it might be related to how the Load Balancer handles idle connections. I've configured the Load Balancer with the following settings: - Backend set to TCP - Health check protocol set to TCP as well - Idle timeout set to 30 seconds (default) However, I read that WebSocket connections are more sensitive to timeouts. I tried increasing the idle timeout to 300 seconds, but I'm still facing disconnections. In my Node.js application, I'm using the `ws` library for WebSocket handling and the following setup: ```javascript const WebSocket = require('ws'); const server = new WebSocket.Server({ port: 8080 }); server.on('connection', (ws) => { ws.on('message', (message) => { console.log(`Received: ${message}`); }); ws.send('Welcome to the WebSocket server!'); }); ``` Additionally, I'm using the `express` framework to serve the application, with a simple endpoint that initiates the WebSocket connection: ```javascript const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('WebSocket Server Running'); }); app.listen(3000, () => { console.log('HTTP server running on port 3000'); }); ``` I've also verified that the WebSocket connections are being established correctly and monitor the connection states. However, when I test the connection and leave it idle, it gets disconnected after the timeout, leading to a frustrating user experience. What steps can I take to ensure the WebSocket connections remain stable and do not get dropped by the OCI Load Balancer? Are there any specific configurations or best practices for handling WebSocket connections in OCI that I might be missing? What's the best practice here? How would you solve this?