Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Flask Session in Gevent Socket-IO

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 .

like image 909
Recursive Avatar asked Aug 12 '13 13:08

Recursive


1 Answers

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.

like image 117
xli Avatar answered Oct 31 '22 09:10

xli