I am trying to make a chat with express.js & socket.io.
Currently, users in my chat can open multiple tabs on the same browser and can chat over all these tabs. This means that the same client can chat with a few people at the same time. I want to prevent this behavior.
I want to prevent a client from connecting to chat everytime when:
1) client works on different tabs on the same browser
2) client opened different browsers and works over them
How i can do this?
Thanks!
You can detect the same user from different tabs in the same browser by using cookies to identify a specific browser. Upon first connection (either socket.io or an express request), you set a unique cookie identifier and then on subsequent connections, you check to see if that unique cookie identifier is already in use. You will have to define in your particular application what "in use" means. For socket.io connections, that's a little easier because it would mean that there is currently already an active connection. For Express requests, that's a little harder because requests are temporal so you have to decide how you tell the difference between different tabs vs. just one tab navigating to a new page (that's app-specific how you might do that).
For detecting multiple browsers, you will have to have user authentication and either assume users don't make multiple accounts or have some sort of system for preventing the same user from making multiple accounts. You then use an account authentication credential in a cookie just like in the previous example to tell if a session for this user is already being used.