Wednesday 21 October 2020

cant connect to node server via socket.io after mapping ip:port address to a domain withouth port

im very new to nodejs so bare with me

so i have a nodejs server which i use to inject live data to my php/laravel website via socket.io i run my nodejs server in port 3002 and apache is using port 80

so my node server is running on

http://78.47.222.225:3002

so i noticed clients who use vpn or proxy cant connect to my nodeserver address and websocket connection fails for them

after asking in this question it was suggested its bcuz vpn is fire walling unusual ports

nodejs server doesn't respond when clients use vpn or proxy

so i maped a address to 3002 port , so basically

http://bia2roll.com/server ---proxying---> http://78.47.222.225:3002

so now when i go to http://bia2roll.com/server i get

Hello World!

which means its working but in my homepage http://bia2roll.com when i try to connect to the server via socket.io it fails ... homepage source

<html>
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js" ></script>

</head>
<body>
<script>

    const  socket = io.connect("http://bia2roll.com/server" ,  {transports:['websocket']});

    socket.on('connect', function () {
        
        console.log('connected!!!') ; 
        
    });

</script>

</body>
</html>

not sure why is this happening but in my console i see it trying to connect to this address

WebSocket connection to 'ws://bia2roll.com/socket.io/?EIO=3&transport=websocket' failed: Error during WebSocket handshake: Unexpected response code: 301
r.doOpen @ index.js:83

but shouldn't it try the bia2roll.com/server/socket.io ? considering i gave http://bia2roll.com/server to io.connect ?

it works fine when i try connecting via ip:port ... try this address to connect via ip:port instead of proxy address

http://bia2roll.com?port=1

const  socket = io.connect("http://78.47.222.225:3002" ,  {transports:['websocket']});

socket.on('connect', function () {
    
    console.log('connected!!!') ; 
    
});

basically

io.connect("http://bia2roll.com/server")

tries to connect to

ws://bia2roll.com/socket.io

which will fail even tho server works with this address bia2roll.com/server ... i think it should connect to ws://bia2roll.com/server/socket.io ?

but

io.connect("http://78.47.222.225:3002")

will connect to

ws://78.47.222.225:3002/socket.io

and it works !

proxy setting

<VirtualHost xxx.xxx.xxx.xxx:443 >
.....
.....

<Location /server>
            ProxyPass http://localhost:3002/
            ProxyPassReverse http://localhost:3002/
    </Location>

</VirtualHost>


from cant connect to node server via socket.io after mapping ip:port address to a domain withouth port

No comments:

Post a Comment