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!
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!
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.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
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.
Heya,
Check if the port on your Droplet is indeed not being used by anything else.
You can do that by running
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:
lsof -i:3002
and found nothing, let’s double-check by also using thenetstat
command: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.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:
If you see any Node.js processes running that shouldn’t be, you can kill them using:
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:
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 to127.0.0.1
(localhost):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