Reputation: 1164
I have a Node / express app, using Passport for account creation and authentication. I'd like be able to check which sessions are active, ie., which users are currently logged in.
I could have a flag on each user's profile which is changed on login / logout, but I don't know how passport handles users who just close the browser and don't specifically go to the logout route, so I don't know if this flag would be set to false in that case.
What's the best way of achieving this?
My end game is to be able to tell the serve whether or not to send a notification email to a other user when they receive a message from someone else - if they're currently logged it, they won't get an email.
Upvotes: 0
Views: 996
Reputation: 36319
Ok, so if you have Passport set up, then the answer depends on what session storage you're using. Essentially, you'll query your session store to see what active users are there, but the actual mechanics of doing so will vary depending on your implementation. Once you know if a given user is considered active by the session store, you can decide to send the message or not.
However, the session store isn't infallible. No matter how you have your session expiration setup, it's almost impossible to guarantee that the store has perfect knowledge of all users; there is invariably some non-zero amount of time from when a user closes a browser window to when the session considers him or her offline.
So, even with that preemptive attempt to make sure a user is online before sending a message, if you want real reliable delivery, you will want to have your client code send back an acknowledgement of receipt. If the ack fails, then you can safely assume the user didn't get it and send the email after the fact.
Upvotes: 1