Could I update graph only once new data arrives

Is publish-subscribe pattern available in Dash? Is it possible to update a graph once new data is available? I am aware of dcc.Interval component, but that’s not suitable for what I need as this triggers updates at regular time intervals. I need to update my graph once data source produces a new piece of data.

I’ve got a number of sensors (temperature, presence, etc) taking measurements in their own time and at different frequencies. Each one has a threshold and it should raise an alarm if the corresponding threshold is broken. And, at that point I’d like to update the graph.

Can I do that in Dash? If so, how? Any pointers/examples would be truly appreciated.

I am not aware of any true publish-subscription solution for Dash at the moment. Do you need to update the graph very fast? Otherwise, i don’t see why you cannot just pull the new data using an Interval component, e.g. once per second.

1 Like

Sensors are wrapped in a code that sends signals at irregular intervals. Most of the time I need refresh every 1s, 5s or even every minute. However, in certain situations (around critical threshold values), I would like the refresh rate at much higher frequencies. Some sensors could produce data at 50Hz, although 10Hz would work.

To set interval at 100ms all the time could be/is inefficient and could put unnecessary strain on the resources.

Are you aware of anything that could help?

If you just need to be able to sample the data at high resolution, you could insert the data into a cache, e.g redis, from which your Dash application reads once per second. With this kind of solution, the graph will still be delayed by a second (or whatever interval you choose) though. If this is not good enough, you’ll probably need a two-way connection instead, e.g. via a socket.

One of the motivations behind django-plotly-dash was enabling this sort of behaviour using websockets. As well as per-user Dash client-server connections, you also need to be able to handle the ingestion of data, which was our motivation behind using this server.

Having said that, we’ve found very few use cases where polling with an Interval component isn’t a more practical and simpler solution for the UI part of the process. You might want to consider to what extent you can separate the UI and data collection pieces.

Yeah, this type of decoupled architecture is what we’d recommend even if we had websocket support. With this architecture, the data storage will be very fast and won’t be limited in anyway to the requests to Dash’s front end.