7

I have a backend with two flask servers. One that handles all RESTfull request and one that is an flask-socketio server. Is there a way how I can share the session variables (Logged in User etc.) between these two applications? They do run over different ports if that is important.

How I have understood sessions they work over client session cookies so shouldn't both of these servers have access to the information? If yes how? and if not is there a way to achieve the same effect?

Jason
  • 654
  • 1
  • 11
  • 27
  • Could you use a simple Redis cache which you write a script for each server to access where your cookies are stored and retrieved? You would just have to set one of the two servers (the one not in control most of the time) to check the cache when the request is made... – sconfluentus Mar 18 '17 at 21:19
  • That sounds like an approach but I was thinking if it was possible to directly point both servers to the same session storage path... I only have limited access to the main (RESTfull) server so something not to invasive would be optimal – Jason Mar 19 '17 at 00:13
  • I definitely do not know how to do that myself. I have used python and flask as a way of pushing out live data analysis projects (at a very simple server-side level) and have limited understanding of how the server stuff works. I hope you find an answer that works for you. Redis, if it comes to that, is extremely simple to use and install...and you can probably do it without admin rights (my test environment at work is a sketchy place our IT people would disapprove of) I installed in in 3 minutes no admins....on AWS. – sconfluentus Mar 19 '17 at 01:33

2 Answers2

5

There are a couple of ways to go at this, depending on how you have your two servers set up.

The easiest solution is to have both servers appear to the client on the same domain and port. For example, you can have www.example.com/socket.io as the root for your Socket.IO server, and any other URLs on www.example.com going to your HTTP server. To achieve this, you need to use a reverse proxy server, such as nginx. Clients do not connect directly to your servers, instead they connect to nginx on a single port, and nginx is configured to forward requests the the appropriate server depending on the URL.

With the above set up both servers are exposed to the client on the same domain, so session cookies will be sent to both.

If you want to have your servers appear separate to your client, then a good option to share session data is to switch to server-side sessions, stored in Redis, memcache, etc. You can use the Flask-Session to set that up.

Hope this helps!

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • 1
    I am using Flask-Session to store session data on redis for both applications and it is getting stored in redis. How do I access session data across applications? – D Dhaliwal Aug 31 '18 at 04:54
0

I found that flask.session.sid = sid_from_another_domain works fine in having individual sub domains case.

I have several flask apps has individual domain names like A.domain.com, B.domain.com, and C.domain.com. They all are based on flask and has redis session manager connected to same redis server.

I wanted to federate them to be logged in at once and to be logged out too.

I had to save the session id on db with user infomation together when I logged in A and passed it to domain B. These domains communicates using oauth2 protocol, and I used flask_dance in this case. And set it into flask.session.sid on B domain. And then I could confirmed that this implementation works fine.