Interval timer on several instances of web interface

Hello all,

I’m building a monitoring app with Dash for the observatory I’m working at.

An essential component of this monitoring is to send alarms if some parameters are not ok with what they should be. For this, I’m using a dcc.Interval to do regular checks and, if not okay, an alarm is send via our radio system through an external python script.

The issue I’m having is that if several people have the web interface open on their computer, each of them is going to send the alarm, which is a bit overwhelming when the radio get 5 alarms in the span of 30 seconds…

Would there be a way to prevent an interval to start if it’s already started in another browser (another computer) ? Or are there other possibilities to do regular checks inside a dash application ?

Simple code snippet (in case I’m doing something the wrong way) :

app.layout =(
    dcc.Interval(
        id='alarm-checks-interval',
        interval=(60 * 1000),
        n_intervals=0
    ),
    html.Div([
        sidebar,
        content,
    ]))

@callback(Input(component_id='alarm-checks-interval', component_property='n_intervals'))
def check_json(n):
    if alarm:
        radio_alarm.send_alarm()

Let me know what would be the different options.

Thanks a lot for the support,

Antoine

Hello @Aprots

You could use an id for the alarm and verify if it needs to send it again, just update a small db with the id of the alarm and whether or not it was triggered. If so, ignore the duplicates.

That sounds like a good workaround, thanks for your input, I’ll work in this direction then.

I would have another question.

If I understand correctly how dash works, when starting the server, it will get “GET” or “POST” requests from the client started in a browser. Is there a way to have the server to send a notification to all connected clients ?

For example, the user would have the possibility to click a button to turn ON/OFF the alarm system. Would it be possible, when this action is made, to notify the other clients that the alarm state has been changed ?

There could be a file or DB containing the alarm state that is checked to display the alarm state on your client, but then, if this file is read at a certain interval, there could be time that you leave your monitoring thinking the alarm is ON (someone turned OFF the alarm but the state file hasn’t been checked yet by your client). I don’t know if this is clear enough…

It is related to the same application but maybe it should be a different topic, sorry if that’s the case.

You are looking into the realm of websockets, which is supported very soon natively by Dash. Essentially, it allows the server to push data to the client whenever it wants to.

That’s exactly what I’m looking for, thanks for the quick answer. Is there a timeline on this ? I will work with some status files to read as a temporary solution.

Hi @Aprots

There is a pre-release available now: dash 4.2.0rc3.
The docs are being worked on currently and should be available soon.

If you want to get a head start, you can find that release on this branch GitHub - plotly/dash at v4.2 · GitHub

Thank you @AnnMarieW, I managed to upgrade to v4.2.0rc3.

I will try to have a look eventhough I don’t think my python skills are advanced enough to work without the docs.

hi @Aprots
The docs will be out very soon, like Ann Marie said.

Thank you for upgrading to the release candidate.
Feel free to ask more questions here in forum if you get stuck in the future.

@Aprots

The new docs for websockets are now available: