Skip to content

feat: fire service events for resent, resync and out-of-sync messages - #25960

Merged
Artur- merged 1 commit into
mainfrom
feat/uidl-recovery-events
Sep 29, 2026
Merged

Artur- merged 1 commit into
mainfrom
feat/uidl-recovery-events

Conversation

@totally-not-ai

@totally-not-ai totally-not-ai Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The server now fires an event on the service event bus in three cases: the client re-sends a message, the client asks for a UI resync, or the client sends a message with an unexpected id. Before, the server only logged these cases. Now apps can react to them, for example to track connection problems.

What changed

  • New event classes: ClientMessageResentEvent, UIResynchronizationEvent and MessageIdSyncErrorEvent, all in com.vaadin.flow.server.communication.
  • ServerRpcHandler.handleRpc fires each event through VaadinService.getEventBus(). It runs on the request thread while the session is locked.
    • ClientMessageResentEvent fires before the old response is sent again.
    • MessageIdSyncErrorEvent fires before MessageIdSyncException is thrown. It includes the expected id and the received id.
    • UIResynchronizationEvent fires before the UI is resynchronized.
  • Backward compatible. Only new API is added. Apps that do not listen see no change.

Use case

An operations team wants to see how often users have unstable connections, so they can find network or proxy problems in production. They register listeners at startup and increase metric counters:

public class ConnectionMetricsInitListener implements VaadinServiceInitListener {
    @Override
    public void serviceInit(ServiceInitEvent event) {
        var bus = event.getSource().getEventBus();
        bus.addListener(UIResynchronizationEvent.class,
                e -> metrics.increment("ui.resync"));
        bus.addListener(ClientMessageResentEvent.class,
                e -> metrics.increment("ui.message.resent"));
        bus.addListener(MessageIdSyncErrorEvent.class,
                e -> log.warn("Out-of-sync message: expected {}, got {}",
                        e.getExpectedId(), e.getReceivedId()));
    }
}

API Changes

3 classes added, 3 constructors added, 5 methods added

com.vaadin.flow.server.communication.ClientMessageResentEvent

// Added
public class ClientMessageResentEvent extends EventObject
public ClientMessageResentEvent(UI ui)
public UI getUI()

com.vaadin.flow.server.communication.MessageIdSyncErrorEvent

// Added
public class MessageIdSyncErrorEvent extends EventObject
public MessageIdSyncErrorEvent(UI ui, int expectedId, int receivedId)
public UI getUI()
public int getExpectedId()
public int getReceivedId()

com.vaadin.flow.server.communication.UIResynchronizationEvent

// Added
public class UIResynchronizationEvent extends EventObject
public UIResynchronizationEvent(UI ui)
public UI getUI()

Test summary

RPC handling (ServerRpcHandler):

  • Fires one UIResynchronizationEvent with the UI on a resync request
  • Fires one ClientMessageResentEvent with the UI on a duplicate message
  • Fires one MessageIdSyncErrorEvent with the correct UI, expected id and received id on an unexpected message id
  • Not covered: the order of each event compared to the resync or the exception

@totally-not-ai

Copy link
Copy Markdown
Contributor Author

Type of change

  • Feature
API changes

New classes in com.vaadin.flow.server.communication, all extending
java.util.EventObject:

  • ClientMessageResentEvent(UI ui) with getUI()
  • UIResynchronizationEvent(UI ui) with getUI()
  • MessageIdSyncErrorEvent(UI ui, int expectedId, int receivedId) with
    getUI(), getExpectedId(), getReceivedId()

Usage:

service.getEventBus().addListener(UIResynchronizationEvent.class,
        event -> resyncCounter.increment());

@sonarqubecloud

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

 1 551 files   - 15   1 635 suites   - 15   1h 40m 31s ⏱️ +13s
12 465 tests  -  1  12 393 ✅  -  1  72 💤 ±0  0 ❌ ±0 
12 864 runs   - 47  12 792 ✅  - 47  72 💤 ±0  0 ❌ ±0 

Results for commit cb537c88. ± Comparison against base commit 253bd55.

@heruan heruan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Each event fires only in its own branch of handleRpc (the unload beacon does not trigger MessageIdSyncErrorEvent), and a listener that throws cannot break the resend, sync error or resync handling, because the event bus logs listener errors. This lets Observability Kit drop its second parse of the UIDL body (vaadin/observability-kit#416). No changes needed.

@heruan heruan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving, ready to merge.

@Artur-
Artur- added this pull request to the merge queue Sep 29, 2026
Merged via the queue into main with commit 83de214 Sep 29, 2026
59 checks passed
@Artur-
Artur- deleted the feat/uidl-recovery-events branch September 29, 2026 11:15
vaadin-bot added a commit to vaadin/docs that referenced this pull request Sep 29, 2026
Add a Message Synchronization Events section to the Service Event Bus
page covering ClientMessageResentEvent, MessageIdSyncErrorEvent, and
UIResynchronizationEvent, fired by ServerRpcHandler.

Documents vaadin/flow#25960 (`cb537c88da418bd09b1dcead1e8a2668f6ea9851`).
@github-actions

Copy link
Copy Markdown
Contributor

Pull request created: #6204

Generated by Documentation Bot · agent · 99.5 AIC · ⌖ 5.62 AIC · ⊞ 11.7K

@github-actions

Copy link
Copy Markdown
Contributor

Documentation Bot: Draft documentation pull request for this change: vaadin/docs#6204

Files updated:

  • articles/flow/advanced/session-lock-and-rpc-listeners.adoc

It was written from the state of this pull request as you see it now. Please review it and mark it ready for review.

Generated by Documentation Bot for #25960 · agent · 99.5 AIC · ⌖ 5.62 AIC · ⊞ 11.7K · ◷

Artur- pushed a commit that referenced this pull request Oct 5, 2026
…#25960) (CP: 25.3) (#26187)

This PR cherry-picks changes from the original PR #25960 to branch 25.3.
---
#### Original PR description
> ## Summary
> The server now fires an event on the service event bus in three cases:
the client re-sends a message, the client asks for a UI resync, or the
client sends a message with an unexpected id. Before, the server only
logged these cases. Now apps can react to them, for example to track
connection problems.
> 
> ## What changed
> - New event classes: `ClientMessageResentEvent`,
`UIResynchronizationEvent` and `MessageIdSyncErrorEvent`, all in
`com.vaadin.flow.server.communication`.
> - `ServerRpcHandler.handleRpc` fires each event through
`VaadinService.getEventBus()`. It runs on the request thread while the
session is locked.
> - `ClientMessageResentEvent` fires before the old response is sent
again.
> - `MessageIdSyncErrorEvent` fires before `MessageIdSyncException` is
thrown. It includes the expected id and the received id.
>   - `UIResynchronizationEvent` fires before the UI is resynchronized.
> - Backward compatible. Only new API is added. Apps that do not listen
see no change.
> 
> ## Use case
> An operations team wants to see how often users have unstable
connections, so they can find network or proxy problems in production.
They register listeners at startup and increase metric counters:
> 
> ```java
> public class ConnectionMetricsInitListener implements
VaadinServiceInitListener {
>     @OverRide
>     public void serviceInit(ServiceInitEvent event) {
>         var bus = event.getSource().getEventBus();
>         bus.addListener(UIResynchronizationEvent.class,
>                 e -> metrics.increment("ui.resync"));
>         bus.addListener(ClientMessageResentEvent.class,
>                 e -> metrics.increment("ui.message.resent"));
>         bus.addListener(MessageIdSyncErrorEvent.class,
> e -> log.warn("Out-of-sync message: expected {}, got {}",
>                         e.getExpectedId(), e.getReceivedId()));
>     }
> }
> ```
> 
> ## API Changes
> 3 classes added, 3 constructors added, 5 methods added
> 
> ### com.vaadin.flow.server.communication.ClientMessageResentEvent
> 
> ```java
> // Added
> public class ClientMessageResentEvent extends EventObject
> public ClientMessageResentEvent(UI ui)
> public UI getUI()
> ```
> 
> ### com.vaadin.flow.server.communication.MessageIdSyncErrorEvent
> 
> ```java
> // Added
> public class MessageIdSyncErrorEvent extends EventObject
> public MessageIdSyncErrorEvent(UI ui, int expectedId, int receivedId)
> public UI getUI()
> public int getExpectedId()
> public int getReceivedId()
> ```
> 
> ### com.vaadin.flow.server.communication.UIResynchronizationEvent
> 
> ```java
> // Added
> public class UIResynchronizationEvent extends EventObject
> public UIResynchronizationEvent(UI ui)
> public UI getUI()
> ```
> 
> ## Test summary
> **RPC handling (ServerRpcHandler):**
> - Fires one `UIResynchronizationEvent` with the UI on a resync request
> - Fires one `ClientMessageResentEvent` with the UI on a duplicate
message
> - Fires one `MessageIdSyncErrorEvent` with the correct UI, expected id
and received id on an unexpected message id
> - Not covered: the order of each event compared to the resync or the
exception

Co-authored-by: totally-not-ai[bot] <290682512+totally-not-ai[bot]@users.noreply.github.com>
caalador pushed a commit to AzazelSensei/flow that referenced this pull request Oct 20, 2026
…n#25963)

Related to vaadin#25960

**missing API** · flow-server, flow-data · monitoring tools listening to
invocation and data events

A listener that wants to know how long an RPC invocation, data fetch or
count took, whether it failed, or which component an RPC targeted, had
to pair the started and ended events in thread locals and look up the
component through the internal state tree. The ended events now carry
the duration Flow measured, the RPC ended event carries the error, and
every RPC event can give the targeted component.

**Risks:**
- ⚠️ Public API: new getters and constructors on existing events. The
  existing constructors stay and report a zero duration and no error.
- ⏱️ Performance: data fetches and counts no longer build events when
  nothing listens, which is a small saving; timing is only done when a
  listener is registered.
- ✅ No breaking change, no serialization, threading, memory or security
  impact: the error field is transient like on the failed event.

- Added `getDuration()` to `RpcInvocationEndedEvent`,
  `DataFetchEndedEvent` and `DataCountEndedEvent`, measured from just
  before the started event
- Added `getError()` to `RpcInvocationEndedEvent`, matching
  `RpcInvocationFailedEvent#getError()`
  - Data events already report a failure as `-1` rows or count, which is
    now documented instead of adding a second accessor for it
- Added `getComponent()` to `AbstractRpcInvocationEvent`, resolved from
the UI's state tree when called, with the same shape as the data events
- `DataFetchObserver` runs the query directly when nothing listens to
  the events of that query
- Extended `ServerRpcHandlerTest` and `DataFetchEventsTest` to check the
  duration, error and component

Co-authored-by: totally-not-ai[bot] <290682512+totally-not-ai[bot]@users.noreply.github.com>
peholmst pushed a commit to vaadin/docs that referenced this pull request Nov 9, 2026
#6204)

Documentation for vaadin/flow#25960 by `@totally-not-ai`[bot].

> [!NOTE]
> The source pull request is merged, so this documentation describes the
final
> shape of the change. Please review it and mark it ready for review.

**Change categories:** NEW_FEATURE

| File | Change |
|------|--------|
| `articles/flow/advanced/session-lock-and-rpc-listeners.adoc` | Added a
Message Synchronization Events section documenting
`ClientMessageResentEvent`, `MessageIdSyncErrorEvent`, and
`UIResynchronizationEvent`, fired on the service event bus while a
client message is handled. Updated the intro paragraph and front matter
to mention this new event category. |
| `articles/flow/advanced/server-client-communication.adoc` | Corrected
what happens when a client message has an unexpected id: the message is
discarded and a session synchronization error is shown, rather than a
resynchronization being started. |

Auto-generated by the Documentation Bot — review before merging.

> Generated by [Documentation
Bot](https://github.com/vaadin/flow/actions/runs/36560597355) for #25960
· agent · 99.5 AIC · ⌖ 5.62 AIC · ⊞ 11.7K ·
[◷](https://github.com/search?q=repo%3Avaadin%2Fdocs+%22gh-aw-workflow-id%3A+doc-bot%22&type=pullrequests)
> - [x] expires <!-- gh-aw-expires: 2026-10-29T11:22:33.980Z --> on Oct
29, 2026, 11:22 AM UTC

<!-- gh-aw-agentic-workflow: Documentation Bot, engine: claude, model:
agent, id: 36560597355, workflow_id: doc-bot, run:
https://github.com/vaadin/flow/actions/runs/36560597355 -->

<!-- gh-aw-expires-type: pull-request -->

<!-- gh-aw-workflow-id: doc-bot -->
<!-- gh-aw-workflow-call-id: vaadin/flow/doc-bot -->

Working on behalf of Petter

---------

Co-authored-by: totally-not-ai[bot] <290682512+totally-not-ai[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants