Number of active sessions

Hey there, thanks for your thoughtful question! :blush: If you’re running Streamlit on your own private cloud (not Streamlit Community Cloud), Streamlit does not provide built-in server analytics or a direct API to count active sessions out of the box. However, each browser tab or window creates a separate session, and Streamlit manages these using WebSockets, but it doesn’t expose a public API for session tracking in self-hosted deployments according to the official docs and architecture guides (Streamlit Architecture).

For custom analytics, you can use workarounds like:

  • Modifying the Tornado server code to log session connections (e.g., by tracking session_id in _BrowserWebSocketHandler.open()), or
  • Using Prometheus metrics at the /metrics endpoint to count sessions, as described in this forum post, or
  • Implementing a simple session counter in your app using a global variable or file/database, but this is not robust for concurrent/multi-process deployments.

If you want to try the Prometheus approach, check out the code snippet in the forum post for counting sessions. For more advanced analytics, consider integrating with external tools like Grafana or custom logging.

Sources: