Using Flask Session in Gevent Socket-IO

919 views Asked by At

Good Morning,

I have been having some trouble and I am hoping some one will be able to help me out a bit here. What I am trying to do is access the Flask Session within a Gevent Socket-IO Name space.

I have tried to use beaker as a middle ware client and I am still unable to access & update the session information from within a socket-io name space.

I am pretty new to all of this so any help would be greatly appreciated .

1

There are 1 answers

0
xli On

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.