'socket.io - How to check if the client has connected from browser

What is the most reliable way to detect if some one has directly connect to my socket connection instead of browser? I know that we can bypass all of that but I realized some WebSockets like tradingView return 403 when connecting directly from socket.io-client but works fine from browser.

How does it detect that And How can I implement that for my own socket.io server?



Solution 1:[1]

I'm not sure how to get device/browser/app the client is connected with but I managed to do something like that using handshake query of socket.io. I needed to distinguish between admin and client so in my client side I used this code:

 const socket = io({
        query: {
            isAdmin: true,
            name: "GetSub Admin"
        },
        // path: '/websockets',
        resource: '/websockets'
    });

and on the server side I used

if (client.handshake.query.isAdmin == 'true') {
            admins.push({
                id: client.id,
                name: client.handshake.query.name
            });
        }
        else {
            users.push({
                id: client.id,
                name: client.handshake.query.name
            });

maybe this will help you.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ali Hasnain