0

I'm making a chat web app. Just found out that if user closes the tab or browser directly without clicking 'logout' button, the state in database will still show "online". So how to logout user when browser or tab is closed? I'm using COOKIE instead of session. I searched a lot in stack overflow, didn't find a working solution.

Anyone can help me? Thanks in advance!

Vincent
  • 1
  • 1
  • Have you tried setting a cookie which expiry date in the past! Check this: http://stackoverflow.com/questions/1783302/clear-cookies-on-browser-close. Clearly you have not searched enough on SO! – Anubhab Apr 09 '16 at 03:37
  • 2
    @Anubhab You could try an Ajax call for onclose, but you should really make sure you have a reasonable session timeout. – HenryDev Apr 09 '16 at 03:40
  • See this: http://www.codeproject.com/Tips/566217/Session-killing-logout-in-server-when-browser-is-c – Manish62 Apr 09 '16 at 03:58
  • answer to your question is here http://stackoverflow.com/a/1783307/3836908 – sanoj lawrence Apr 09 '16 at 05:18

1 Answers1

0

A lot of people in the comments seem to have misunderstood your question. Even though session cookies will be deleted when you close your browser this doesn't tell your web app that the session is over.

The only real way of reliably detecting if the user has your web app open is to make a frequent AJAX call that updates a database entry to say when the user was last seen. If they miss several expected AJAX calls you can assume they are offline.

Another possibility is a web-socket that you use to update the users status on disconnect.

You will probably need to implement one of these systems anyway to get new chat messages so you might as well just integrate a system for checking when the user was last seen in to that.

Chris
  • 5,571
  • 2
  • 20
  • 32