CodexBloom - Programming Q&A Platform

Node.js server process exiting unexpectedly with 'ECONNRESET' when handling multiple WebSocket connections

👀 Views: 350 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-27
node.js websocket error-handling JavaScript

I'm sure I'm missing something obvious here, but Hey everyone, I'm running into an issue that's driving me crazy... I'm experiencing an scenario where my Node.js server is exiting unexpectedly when handling multiple WebSocket connections. I'm using `ws` version 8.2.0 for WebSocket implementation, and my server code looks like this: ```javascript const WebSocket = require('ws'); const server = new WebSocket.Server({ port: 8080 }); server.on('connection', (socket) => { socket.on('message', (message) => { console.log('received: %s', message); socket.send('Hello from server'); }); socket.on('close', () => { console.log('Connection closed'); }); }); server.on('behavior', (behavior) => { console.behavior('WebSocket behavior: ', behavior); }); process.on('uncaughtException', (err) => { console.behavior('Uncaught Exception: ', err); }); ``` The scenario manifests when around 50 concurrent connections are active. I see logs in the console indicating `behavior: read ECONNRESET` and shortly after, the server process exits with a non-zero status code. I've wrapped my code with `try...catch` and added an `uncaughtException` handler, but the server still crashes. I've tried increasing the maximum number of connections allowed, adjusting the timeout settings, and ensuring that my server is handling backpressure properly, but the scenario continues. Any insights on why this might be happening or how to prevent the server from crashing in such scenarios would be greatly appreciated. Thanks in advance! My team is using Javascript for this desktop app. What am I doing wrong?