Using Flash Sessions in Gevent Socket-IO

Good morning,

I had some problems and I hope someone can help me a little here. What I'm trying to do is access the Flask session in the Gevent Socket-IO namespace.

I tried using beaker as a mid-level client, and I still cannot access and update session information from the io socket namespace.

I am new to this, so any help would be greatly appreciated.

+6
source share
1 answer

In a default flash drive session, it is possible to share read-only access to session data during namespace initialization.

session_dict=dict(session) socketio_manage(request.environ, {'/news': NewsNamespace}, request=session_dict) 

session_dict then available as self.request in the namespace. This is a hack, but Flask / werkzeug sessions do not seem to work outside of the request context.

I put a session id (randomly generated uuid) in a Flask session. Then, both the flash drive view and the namespace know the session identifier, and both can read / write data in the server-side session in Redis or another data store with the session identifier as the key.

This is just the solution I use to exchange sessions between Flask and gevent-socketio; I have not tried Beaker and do not know what specific problems you had in use, or if there is a better solution.

+3
source

Source: https://habr.com/ru/post/951559/


All Articles