In a IoT control app I need to limit the access to ONE session. If more than one user connect to a motor controller s/he could send contrasting commands that could damage the system.
The idea was to save in a global session_state the number of connections, and then check at each connection if there is already a connected user.
What I understand about it (and given my limited knowledge I could be wrong):
Each connection has its own st.session_state, while I need a state shared among connections.
I used the library streamlit_server_state that can share values among sessions, but it stopped working after some time because the state lock was faulty and froze the app till I had to restart my PC. It is something that should never happen in real life.
I thought to save the number of users in Redis, but when a browser tab is closed I need to decrease it accordingly otherwise all future connection will be negated.
Is mine a one-time problem that will never happen again in the Universe history, or did somebody already managed to solve it?
Can you have your IoT device itself accept/reject connections, or use a message queue? Then you wouldn’t be limiting the connections to your streamlit app, but instead handling within Streamlit an accepted or rejected response from the IoT device?
Otherwise, I’d imagine setting locks to auto-expire. Let’s say one minute of inactivity will disconnect the user.
User starts a new session → generate a random key to serve as an id for them, or even IP address+starttime…just something unique
Check your save location for an unexpired lock.
If there is an unexpired lock under a different key/ID, reject the user
If there is an unexpired lock under the same key/ID, update the expiration to one minute out
If there is no unexpired lock, create a new one under current user’s key/ID and set it one minute out
Thank you for your answer.
A heartbeat every second or so to keep the connection alive is my solution at the moment. After the tab is closed and there is no heartbeat for 3 seconds, the connection is available to another session.
I hoped there is a way to set it directly from Streamlit or Tornado.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.