Question

bogus EADDRINUSE on web socket opening (nodeJS on a droplet)

Hello,

I have a nodeJS app which works fine locally. It listens to localhost:3001 for Http/web traffic, and then establishes a websocket connection on localhost:3002 for web sockets. Everything works fine when running locally, and the nginx proxy through to localhost:3001 works fine for the http connection.

The problem is the websocket connection. The statement const wsS = new WebSocketServer({ 3002, clientTracking: true }); works fine locally.

But on my Droplet it barfs with:

WSServer.e: {"code":"EADDRINUSE","errno":-98,"syscall":"listen","address":"::","port":3002}

I am sure that there is nobody else using that port - I can run this right after a machine reboot and get the error. also (thanks to an old support thread for this) “lsof -i:3002” shows nothing. And “pm2 list” also shows nothing.

So what’s it bitching about? :). Any attempt to then use that web socket fails. I’m getting the error running “node index.js” on the box without even pinging it from outside. I can ping the web server from outside, so I think the nginx proxying is ok, BTW.

Oh and here’s the ufw output:

To                         Action      From
--                         ------      ----
22/tcp                     LIMIT       Anywhere
Nginx Full                 ALLOW       Anywhere
OpenSSH                    ALLOW       Anywhere
80/tcp                     ALLOW       Anywhere
22/tcp (v6)                LIMIT       Anywhere (v6)
Nginx Full (v6)            ALLOW       Anywhere (v6)
OpenSSH (v6)               ALLOW       Anywhere (v6)
80/tcp (v6)                ALLOW       Anywhere (v6)

Thanks in advance!


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

IT WORKS! Thanks so much for your fantastic support. The problem was, of course, mine: I had thought that nginx removed the “/be” as part of the forwarding. It doesn’t. So changing to, as you suggested, app.use(‘/be’, …)

did it. Now everything works, websockets or no.

many thanks again, this issue can be closed.

KFSys
Site Moderator
Site Moderator badge
September 12, 2024

Heya,

Check if the port on your Droplet is indeed not being used by anything else.

You can do that by running

netstat -tulpen | grep 3002

Also make sure your Nginx is porperly configured to use that websocket.

You can paste your Nginx config to see if we can assist with that.

Hey 👋

The error you’re encountering (EADDRINUSE) typically indicates that the port you’re trying to use (3002 in this case) is already in use by another process.

Keep in mind that you can not have multiple services listening on the same port.

What you could do to verify if this is the case is:

  1. Although you’ve already checked with lsof -i:3002 and found nothing, let’s double-check by also using the netstat command:
sudo netstat -tuln | grep 3002

That command should help confirm whether any process is indeed using port 3002. If nothing shows up, it suggests the port is not actively in use, but the issue might still persist due to other factors.

  1. Sometimes, Node.js processes can linger, especially if you’ve been running and stopping the server multiple times. Use the following command to check if there are any rogue Node.js processes:

    ps aux | grep node
    

    If you see any Node.js processes running that shouldn’t be, you can kill them using:

    sudo kill -9 <PID>
    

    Replace <PID> with the actual process ID.

Then give it a try again after killing the process.

If this still does not work, can you share your Nginx config here so I could take a look?

Basically, if your WebSocket traffic should be handled by Nginx, you have to make sure that you’ve configured the Nginx server block correctly to proxy WebSocket connections to your Node.js app. Here’s an example of how you might configure Nginx for WebSocket proxying:

server {
    listen 80;
    server_name your_domain.com;

    location / {
        proxy_pass http://localhost:3001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location /websocket {
        proxy_pass http://localhost:3002;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Also one more thing that you should check is that by default, new WebSocketServer({ port: 3002 }) will try to bind to all available IP addresses (both IPv4 and IPv6). If there is a conflict or an issue with binding to :: (all IPv6 addresses), you can try binding the WebSocket server specifically to 127.0.0.1 (localhost):

const wsS = new WebSocketServer({ host: '127.0.0.1', port: 3002, clientTracking: true });

Additionally, I would suggest checking the Node.js application logs and Nginx error logs (/var/log/nginx/error.log) for any additional clues.

Let me know how it goes!

- Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more