How to keep session alive for longer

I’m self-hosting a streamlit app and have users complain that the session state is lost if they step away from the computer for some time. What is a good way for dealing with this?

I thought about saving the session_state to disk, but some of the entries are not serializable.

Then there is the st_autorefresh component. Is that the recommended way?

Or can I just change some configuration paramter, such that the session stays alive for longer?

What platform/OS/service are you using to self-host? If I run a Streamlit app locally, I can leave it overnight without losing state. Although, some elements, like st.file_uploader can cause a little mayhem when trying to keep a session running.

Also, your users’ browser settings can come into play. If a websocket disconnects and doesn’t quickly reconnect, then the session will be lost. This is especially common on mobile devices or if a device goes to sleep. (If you scale horizontally by creating multiple Streamlit servers and don’t have custom routing, a websocket reconnect could reset even an active session if it doesn’t coincidentally reconnect to the exact same server.)

Besides st_autorefresh, you can also define a trivial fragment with run_every defined. st_autorefresh will rerun the app, but a fragment could rerun a basically empty function for efficiency. Depending on your and your users’ precise situation, those may or may not address your problem, though.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.