Thu Sep 12 00:22:08 UTC 2024: ## Node.js WebSockets Fail on Droplet: Common Error & Solutions

**A DigitalOcean user is facing an issue where their Node.js application’s websocket connection fails on their Droplet, despite working locally.** The error message “EADDRINUSE” suggests the port (3002) is already in use.

**The user has confirmed that no other process is using the port**, ruling out the most obvious cause. The problem could stem from lingering Node.js processes or a misconfigured Nginx proxy.

**The community suggests the following solutions:**

1. **Check for lingering Node.js processes:**
– Use `ps aux | grep node` to list running Node.js processes.
– Terminate any unnecessary processes using `kill ` (replace with the process ID).

2. **Review Nginx configuration:**
– Share the Nginx configuration for review.
– Ensure proper configuration for proxying WebSocket connections to the Node.js application.

3. **Bind WebSocketServer to localhost:**
– Use `const wsS = new WebSocketServer({ port: 3002, clientTracking: true, host: ‘127.0.0.1’ });` to specifically bind to the localhost address.

4. **Examine application and Nginx logs:**
– Check `Node.js application logs` and `/var/log/nginx/error.log` for further clues.

**DigitalOcean provides a detailed explanation of the error and offers practical solutions for developers facing this common issue.** The article emphasizes the importance of proper port management and Nginx configuration when working with websockets in a cloud environment.

**This article is a valuable resource for developers encountering similar problems while deploying Node.js applications on DigitalOcean.** It highlights the importance of troubleshooting common errors and provides practical advice on resolving them.

Read More