How to scheduled an UI update

I’m going to implement a session scheduler for a conference which displays the schedule on a big screen. It shows the running sessions and the following sessions. It will run on a Raspberry Pi attached to the HDMI of the display.

What is the recommended way to update parts of the view regularly, e.g. every minute? I want to update the countdown (“8 minutes left”), remove sessions from the view when ended and add new sessions.

I’m using Vaadin 24.3.8 with Spring Boot 3.2.4 and Java 21.0.2.

You can enable Push on the application and the scheduler from Spring Boot then you can use this code in your MainLayout (or only for this view):

@Scheduled(fixedRate = 1000)
public void scheduleFixedRateTask() {
	getUI().ifPresent(ui -> {
			ui.access(() -> {
       // refresh your page
       }
    }
}

For a countdown, you can probably use a custom component that does the countdown on the client side but that’s probably premature optimization :slight_smile:

1 Like

Thank you very much, this works very well! Maybe you’ll see this at a conference at work soon… :grinning:

1 Like

I don’t like the annotation as it’s not flexible and cannot be stopped.

I prefer to inject TaskScheduler and then use

taskScheduler.scheduleAtFixedRate(...)

3 Likes

Thank you very much for your hint, I refactored my code accordingly. Looks good now and no more exceptions when I close the browser tab. :grinning:

1 Like