Search code examples
javascriptnode.jsnginxsocket.iodigital-ocean

Client unable to hit socket.io server


Project is hosted on Digital Ocean.

On the client side, its throwing a 404 error

GET http://134.209.147.204/socket.io/?EIO=3&transport=polling&t=NKKWF-X //404

Here is the nginx config file

server {
        listen 80;
        root /var/www/html;
        location / {
                proxy_pass http://127.0.0.1:5000; (where the frontend is running)
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }
        location /socket/ {
                proxy_pass http://localhost:3001; (where the sockets.io server is running)

        }

}

Frontend

socket = io('/socket/')

Both the frontend and backend runs without any errors and can be accessed from the browser.


Solution

  • After days of hacking, I was able to make it work!

    nginx config

    upstream websocket {
        server 127.0.0.1:3001;
    }
    
    server {
        listen 80;
        root /var/www/html;
        location / {
            proxy_pass http://127.0.0.1:8080;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    
        location /ws/ {
            proxy_pass http://websocket/socket.io/;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }
    

    socket.io server

    const app = require('express')();
    const server = app.listen(3001);
    const io = require('socket.io').listen(server);
    

    socket.io client

     socket = io.connect('http://yourdomain/', {path: '/ws/'})