miguelgrinberg / flask-sock

Modern WebSocket support for Flask.
MIT License
272 stars 24 forks source link

Sharing session object between Websocket connection and HTTP request #53

Closed mubarakalmehairbi closed 1 year ago

mubarakalmehairbi commented 1 year ago

How can I share flask's session object between a Websocket connection and HTTP request?

As an example, if part of the code looks like this:

from flask import session

@socket.route("/socket"):
def communicate(ws):
    session['number'] = 10

@app.route("/req"):
def view_function():
    return f"<p>session['number']</p>"

It seems the key number will not be shared. How can I allow it to be shared?

miguelgrinberg commented 1 year ago

The websocket protocol does not have a way to set cookies, so it is not possible to update the flask user session from inside the socket route.

To share data between the websocket and http routes you can use your database or maybe a redis store.