0

I'm creating chat website using flask socketio. I control people who join chatting using session ( When user login, I set people's session to usernake)like session ['name']=username And when the user is out of chat, I set his or her session none. But in this situation when the user closes the website by not click logout button, there is the user's session though the user logout.. so the user's name is on the list...

How to manage people who do not logout by closing the website?

jrbedard
  • 3,662
  • 5
  • 30
  • 34
  • Possible duplicate of [this](http://stackoverflow.com/questions/3888902/javascript-detect-browser-close-tab-close-browser) SO question. – Jeroen Heier Jan 15 '17 at 10:43

2 Answers2

0

You might want to try using Javascript to detect a browser close event. It's not a perfect solution, but it will give you a chance in many cases to detect that the user is leaving, and fire off some code to end their session.

You can check out various approaches, but I saw this question which seemed pretty helpful.

Community
  • 1
  • 1
coralvanda
  • 6,431
  • 2
  • 15
  • 25
0

As per this doc http://flask.pocoo.org/docs/0.12/api/ you have this parameter for flask.session

permanent If set to True the session lives for permanent_session_lifetime seconds. The default is 31 days. If set to False (which is the default) the session will be deleted when the user closes the browser.

Also from flask-socketio there is a mentioning of session behaviour

The session context global behaves in a different way than in regular requests. A copy of the user session at the time the SocketIO connection is established is made available to handlers invoked in the context of that connection. If a SocketIO handler modifies the session, the modified session will be preserved for future SocketIO handlers, but regular HTTP route handlers will not see these changes. Effectively, when a SocketIO handler modifies the session, a “fork” of the session is created exclusively for these handlers. The technical reason for this limitation is that to save the user session a cookie needs to be sent to the client, and that requires HTTP request and response, which do not exist in a SocketIO connection. When using server-side sessions such as those provided by the Flask-Session or Flask-KVSession extensions, changes made to the session in HTTP route handlers can be seen by SocketIO handlers, as long as the session is not modified in the SocketIO handlers.

Alexey Smirnov
  • 2,573
  • 14
  • 20