I have a NodeJS backend with Express and WS, and is a simple messenger-like chat app. You login, then you chat. But I want to add a feature on top of it. If a user is already logged and ACTIVE (socket connection exists, not broken) I want it's activeStatus : active in database. So that if he tries to login from a different browser, if his activeStatus is active, he won't be able to connect.
How is this achievable? I cannot understand how am I supposed to link a websocket connection to a specific ID in database, and when connection is on, update db, and when connection drops, update it to that ID again.
Just set a unique parameter for WebSocket instance in wss.on('connection',...) and use it later to identify the user. Also, if you aren't able to link websocket connection to a specified user, how does your authentication work? It seems like anyone can connect to your server, if it can't differentiate between users.