Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io - How to get client URL request on server side?

In my app.js (server) I need to know from where (URL) the request came.

Currently, I'm passing the URL as parameter from the client:

socket.emit('request', window.location.href);

and processing it server side

socket.on('request', function(url){
    console.log(url);
    ...
});

But that's clearly risky and unsecure (clients can send anything to the server).

So I'm guessing.. is it possible to get the URL parameter only on server side? Maybe from the socket object?

like image 750
pumpkinzzz Avatar asked Oct 20 '22 03:10

pumpkinzzz


1 Answers

For the entire URL you could use socket.handshake.headers.referer

io.on('connect', (socket) => {
     console.log(socket.handshake.headers.referer);
});
like image 178
Aidan Hook Avatar answered Nov 04 '22 00:11

Aidan Hook