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.
Can you share an some of your code and implementation details for a streamlit heartbeat to limit concurrent connections?