Streaming Server-side State Changes #1060
-
Hi! I'm looking to see if there is a recommended (or hacky) path to stream data into Mesop's state from the server and have it update on the client. (Without using a client-side trigger to invoke or continuous polling) I'm looking to enable:
Example use case is a stock trading app where we receive live updates from a websocket, gRPC message, message queue (rabbit MQ) or a similar push message on one process that then sends a message to the process Mesop is running on with the updated stock data. Mesop takes this data updates its state and then re-renders the chart (or similar) that shows the live stock chart. Other use cases may involve IoT sensor streaming etc. ie. your thermostats temperature reading in real-time. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
Right now there isn't a great way to do this. You can create a web component that does something like a Are you trying to update the data for all the sessions/users connected or one specific session? We're working on a websockets feature which is still very experimental, but we could eventually add some kind of support to push data from the server to the client. Right now, with the default HTTP SSE (server-sent events) transport that we use in Mesop, there's not a good way to push data from the server to the client. |
Beta Was this translation helpful? Give feedback.
-
Another temporary option could be to create a web component that creates a web socket connection to a say a Fast API server. This Fast API server could receive the push events, which could be forwarded to your web component, which could then trigger an event to Mesop which could then update the UI. Not ideal either since you have some extra hops here and so may not be the best if you need really low latency for stock trading app for example. |
Beta Was this translation helpful? Give feedback.
-
Thanks for all the help! For defining my own web socket (mqtt or similar) on the Python side how would you go about recommending this? Is the idea that we'd define these interfaces in the app logic itself. Looking for an example similar to this one from NiceGUI: https://github.com/zauberzeug/nicegui/tree/main/examples/zeromq where there is a way to hook into the startup process |
Beta Was this translation helpful? Give feedback.
Until we can integrate push support with our experimental Web Sockets integration, you'll have to define the push interfaces outside of the Mesop app.
Basically create FastAPI server (See https://google.github.io/mesop/guides/server-integration/). FastAPI has web socket support (https://fastapi.tiangolo.com/advanced/websockets/) so that may be the easiest integration path. I haven't used MQTT yet, so but if you can make a web socket connection directly, then you could skip the Fast API server part.
Now your custom Web Component will serve as the mediator between your Web Socket server and Mesop App. The Web Component will initialize the web socket connection. For Web Component tutorial, see