Question

Issue emitting socket.io messages on Ubuntu droplet

I recently moved my API:s from Heroku to a Ubuntu droplet provided by digital ocean. I followed these instructions on how to deploy my Node.js/Express API:s and got all of my endpoints working as expected. The only issue is that im able to connect/disconnect to the socket, but not able to emit any messages through the websockets. Everything works fine locally & in heroku with the same code, so I think the issue might be with nginx or ufw firewall.

Client code:

    const socket = socketIOClient(https://www.myapp.com/api-with-websockets);
    socket.connect();
    socket.emit('joinChannel', conversation.id);

Backend:

io.on("connection", (socket) => {
  console.log("New client connected");

  socket.on("joinChannel", (msg) => {
    console.log("New client joined channel:", msg); // <-- never gets called
    socket.join(msg);
  });

  socket.on("disconnect", () => {
    console.log("Client disconnected");
  });
});

nginx conf:

server {

        root /var/www/myapp.com/html;
        index index.html index.htm index.nginx-debian.html;

        server_name myapp.com www.myapp.com;

        location / {
                try_files $uri $uri/ =404;
        }

        location /api-with-websockets/ {
          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_cache_bypass $http_upgrade;

        }


    location /no-websockets-api/ {
        proxy_pass http://localhost:3003/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    location ~ ^/(socket\.io) {
        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_cache_bypass $http_upgrade;
    }


    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/myapp.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/myapp.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}
server {
    if ($host = www.myapp.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = myapp.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80;
        listen [::]:80;

        server_name myapp.com www.myapp.com;
    return 404; # managed by Certbot

}

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.

Accepted Answer

Hi @bobbyiliev ! I managed to fix the issue. Turned out that my client was routing the socket path wrong. I changed the socket options to include resource path to my api and it started working. Thank for your help!

Client:

  const socketIOConnOpt = {
    'force new connection': true,
    reconnection: true,
    reconnectionDelay: 10000,
    reconnectionDelayMax: 60000,
    reconnectionAttempts: 'Infinity',
    timeout: 10000,
    transports: ['websocket'],
    resource: '/conversation-api/',
  };
const socket = socketIOClient('https://www.myapp.com', socketIOConnOpt );

Thanks for the reply @bobbyiliev

The only thing showing up in the Nginx logs is this:

2020/11/26 16:55:42 [alert] 21838#21838: *50 open socket #24 left in connection 6
2020/11/26 16:55:42 [alert] 21838#21838: *42 open socket #17 left in connection 12
2020/11/26 16:55:42 [alert] 21838#21838: *44 open socket #22 left in connection 13
2020/11/26 16:55:42 [alert] 21838#21838: *48 open socket #21 left in connection 14
2020/11/26 16:55:42 [alert] 21838#21838: aborting

I have been stuck with this issue for couple of days now and found this discussion about opening ports. When I run the command telnet google.com 3003 im not able to connect, could that maybe cause this problem? I tried opening the port 3003 ufw firewall and by running iptables -I INPUT 1 -i eth0 -p tcp --dport 3003 -j ACCEPT but still no luck.

Bobby Iliev
Site Moderator
Site Moderator badge
November 26, 2020

Hi there @ollisami,

The configuration actually looks correct.

Do you see any errors in your logs? I could suggest starting with the Nginx error log, first trigger a request, and then use the following command the check the logs:

  1. sudo tail -100 /var/log/nginx/error.log

Let me know how it goes. Regards, Bobby

Try DigitalOcean for free

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

Sign up

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
DigitalOcean Cloud Control Panel