MQTT + Streamlit

Hi,
I found many posts about using MQTT with Streamlit, most of them are outdated, recommending solutions that fails at the first import, a few of them use unmaintainable and undocumented logics.

What I would like to do:

  • Subscribe to a topic with my paho-mqtt client,
  • Receive update with an on_message callback function
  • Update the UI from that function
def on_message(client, userdata, message):
    st.write(message.payload.decode()) 

Why is it a challenge :

  • The on_message is ran in a different thread
  • I canā€™t seem to update the session_state nor any of ui element from this thread
  • Solutions posted around either involve reloading the full stuff periodically, either do not work anymore
  • I get the console flooded by this error:
2024-07-15 16:28:10.639 Thread 'paho-mqtt-client-TEST_DEVICE': missing ScriptRunContext
2024-07-15 16:28:10.639 Thread 'paho-mqtt-client-TEST_DEVICE': missing ScriptRunContext
2024-07-15 16:28:10.639 Thread 'paho-mqtt-client-TEST_DEVICE': missing ScriptRunContext

How should I proceed ?

outdated posts:

unmaintainable logic:

1 Like

Not an answer, but I also wanted to point out:

Related posts:

  1. Using Paho-MQTT in streamlit
  2. Real time data update (using mqtt) - #2 by dataprofessor
  3. How to stop a MQTT subscription with a button while keeping my data?
  4. Live plot from a thread - #2 by thiago