> Markdown version of [Callbacks](https://vaadin.com/docs/next/building-apps/server-push/callbacks). Section index: [llms.txt](https://vaadin.com/docs/next/building-apps/llms.txt)

# Implementing Callbacks

When building the user interface with Vaadin Flow, the easiest way of allowing a background job to update the user interface is through callbacks. This is explained in more detail in the [Callbacks](https://vaadin.com/docs/next/building-apps/business-logic/background-jobs/interaction/callbacks.md) documentation page.

Whenever you implement a callback, remember that the callback is called by the background thread. This means that any updates to the user interface must happen inside a call to `UI.access()`.

> **Note:** The examples on this page only work with push enabled. For information about how to do that, see the [Server Push](https://vaadin.com/docs/next/building-apps/server-push.md) documentation page.

For every callback, you should create a private method in your user interface. The method is called inside `UI.access()`, so you can safely update the user interface inside it.

For example, a method for handling successful completion could look like this:

```java
private void onJobCompleted(String result) {
    Notification.show("Job completed: " + result);
}
```

Likewise, a method for handling errors might be done like this:

```java
private void onJobFailed(Exception error) {
    Notification.show("Job failed: " + error.getMessage());
}
```

For reporting progress, you can use a [progress bar](https://vaadin.com/docs/next/components/progress-bar.md). If the background jobs reports the progress as a floating point value between 0.0 and 1.0, you can pass it directly to the `setValue` method of the progress bar.

Here is an example of a button click listener that starts a background job, and uses the private methods, and the progress bar, to update the user interface:

```java
button.addClickListener(clickEvent -> {
    var ui = UI.getCurrentOrThrow();
    service.startBackgroundJob(
        ui.accessLater(this::onJobCompleted, null), // (1)
        ui.accessLater(progressBar::setValue, null),
        ui.accessLater(this::onJobFailed, null)
    );
});
```

1. The `UI.accessLater()` method is explained on the [Pushing UI Updates](https://vaadin.com/docs/next/building-apps/server-push/updates.md#access-later) documentation page.

> **Tip:** When a callback only updates UI *state* — such as a progress value or a status message — you can update a [signal](https://vaadin.com/docs/next/flow/ui-state.md) from the callback instead of wrapping it in `UI.accessLater()`, and bind the component to that signal. See [Pushing State with Signals](https://vaadin.com/docs/next/building-apps/server-push/updates.md#pushing-state-with-signals).
