Using Flask Session in Gevent Socket-IO Using Flask Session in Gevent Socket-IO flask flask

Using Flask Session in Gevent Socket-IO


With the default Flask session, it seems possible to share (with read-only access) the session data at the time the namespace is initialized.

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

session_dict is then accessible as self.request in the namespace. This is a hack, but Flask/werkzeug sessions don't seem to work outside the request context anyway.

I put a session ID (randomly-generated uuid) in the Flask session. Then, both Flask views and the namespace both know the session ID, and both can read/write data in a server-side session in Redis or some other datastore, with session ID as the key.

This is just the solution I'm using for sharing sessions between Flask and gevent-socketio; I haven't tried Beaker and am not sure what specific issues you've had in using that, or if there's a better solution.