Repository navigation
docs: document watching requests via client.events - #6566
Conversation
Add a section to Server to Client Communication describing window.Vaadin.Flow.clients[appId].events and the vaadin-request event, through which page scripts can observe each request's delivery (sent/error/response/end) and dispatch synthetic events for testing. Documents vaadin/flow#26064 (`3962e2c011fed2b23ed3f3547341980495f0a9ef`).
Preview DeploymentThis PR has been deployed for preview. URL: https://docs-preview-pr-6566.fly.dev Changed pagesAdded content is highlighted in green; removed content is marked in red on each page.
Built from bedbbfe |
vaadin-review-bot
left a comment
There was a problem hiding this comment.
Reviewed the changes — left 5 comments.
| Finding | |
|---|---|
failure is an object with reason and status, not the reason string the page describes |
|
| "every request the client sends" is wrong: heartbeats are not announced | |
| 💬 | The example uses a client variable that the page never defines |
| 💬 | The page omits that listeners run synchronously inside the client's work |
| 📐 | The new section has no since badge |
| A `vaadin-request` event is dispatched when the first invocation for a request is queued, or right before the first send for a request that has no invocations, such as a resynchronization. Its `detail` is the request, which then dispatches events of its own as its delivery proceeds: | ||
|
|
||
| `sent`:: An attempt to deliver the request goes out. The request's `attempt` property counts attempts, starting at `0` while it's still queued. | ||
| `error`:: The attempt failed. The request's `failure` property gives the reason: `network`, `timeout`, `http` with the response's `status`, or `invalid-response` for a reply that couldn't be parsed. |
There was a problem hiding this comment.
failure is an object with reason and status, not the reason string the page describes
The request's
failureproperty gives the reason:network,timeout,httpwith the response'sstatus, orinvalid-response
In flow-client/src/main/frontend/internal/client/communication/VaadinRequest.ts:21-34, failure is a VaadinRequestFailure object: { reason: 'network' | 'timeout' | 'http' | 'invalid-response', status?: number }. The page never names reason or status, so a reader writes request.failure === 'timeout' and the check never matches.
Suggested rewrite:
The attempt failed. The request's
failureproperty gives an object with areasonofnetwork,timeout,httporinvalid-response, plus astatuswith the HTTP status when the reason ishttp.
server-client-communication.adoc:86 · accuracy
There was a problem hiding this comment.
Fixed. failure is now described as an object whose reason is network, timeout, http or invalid-response, with status holding the HTTP status for http.
|
|
||
| == Watching Requests from Page Scripts | ||
|
|
||
| `window.Vaadin.Flow.clients[appId].events` is an `EventTarget` that announces every request the client sends to the server, so a page script can follow one, for example to measure it. The client only dispatches on it and never listens to it, so nothing a page script dispatches on it changes what the client does. |
There was a problem hiding this comment.
window.Vaadin.Flow.clients[appId].eventsis anEventTargetthat announces every request the client sends to the server
Both ClientEvents.ts and VaadinRequest.ts:97 state "Heartbeats are not covered", and Heartbeat.ts sends its own XHR without creating a VaadinRequest. The unload beacon (MessageSender.sendUnloadBeacon()) also bypasses it. A reader who builds a connection monitor on this will wait for events that never arrive.
Suggest "announces the UIDL requests the client sends to the server; heartbeats are not covered".
server-client-communication.adoc:81 · accuracy
There was a problem hiding this comment.
Fixed. The section now says that the client announces the requests it sends, and that heartbeats aren't announced.
|
|
||
| [source,javascript] | ||
| ---- | ||
| client.events.addEventListener('vaadin-request', ({ detail: request }) => { |
There was a problem hiding this comment.
💬 The example uses a client variable that the page never defines
The prose names window.Vaadin.Flow.clients[appId].events, then the example starts from a bare client. The page never says what appId is or how a page script obtains it, and appId appears nowhere else in the documentation. A reader cannot run the example as written.
Adding one line fixes it, for example:
const client = window.Vaadin.Flow.clients[appId];along with a sentence on where appId comes from.
server-client-communication.adoc:94 · clarity
There was a problem hiding this comment.
The example now starts with const client = Object.values(window.Vaadin.Flow.clients).find((c) => c.events);. The prose says that clients holds one client per application, keyed by its application ID. It also says that the script has to run after the application has started, for example through Page.executeJs().
| `response`:: A reply to the attempt arrived and was identified. | ||
| `end`:: The delivery has settled. The request's `outcome` property says how: `acknowledged` for a reply from the server, `rejected` if the server couldn't process the request or the session has expired, or `discarded` if the client gave up without a confirming reply. `end` is dispatched exactly once, as the last event. | ||
|
|
||
| The request is the same object across every attempt to deliver it. A resend after a lost connection, a resend from the resend timer, or a resend over a reconnected push channel all fire `sent` again on the same object, with `attempt` incremented. |
There was a problem hiding this comment.
💬 The page omits that listeners run synchronously inside the client's work
VaadinRequest.ts:93-95 and ClientEvents.ts:67-69 both warn: "Listeners run synchronously inside the client's own work, including while it queues invocations: keep them cheap and do not call back into the client from them." The section invites readers to add listeners for measurement but carries none of this, so an expensive listener or a reentrant call into the client looks supported.
server-client-communication.adoc:90 · clarity
There was a problem hiding this comment.
Added: listeners run synchronously inside the client's own work, including while it queues invocations. Keep them cheap, and don't call back into the client from them.
| This process ensures UI consistency, even in the presence of errors or unexpected behavior. The application remains robust against transient network problems or client-side interruptions. Additionally, the user experience is preserved without requiring a full page reload, as Vaadin rebuilds the UI without reloading the entire bundle from the server. | ||
|
|
||
|
|
||
| == Watching Requests from Page Scripts |
There was a problem hiding this comment.
📐 The new section has no since badge
CONVENTIONS.md: "Mark new features with a since badge instead: the [role="since:com.vaadin:vaadin@V25.2"] attribute above a section heading". The PR is categorized NEW_FEATURE, and the sibling section below carries such a badge. Add the badge for the current development version (25.4 per articles/_vaadin-version.adoc):
[role="since:com.vaadin:vaadin@V25.4"]
== Watching Requests from Page Scriptsserver-client-communication.adoc:79 · conventions
There was a problem hiding this comment.
Added [role="since:com.vaadin:vaadin@V25.4"] above the heading. The feature shipped in 25.4.0-alpha5.
failure is an object with reason and status, and heartbeats aren't announced. Show how a page script gets the client, say that listeners run synchronously, and add the 25.4 since badge.
…6820) ## Summary Documents the two other events that `window.Vaadin.Flow.clients[appId].events` dispatches, next to `vaadin-request`: `vaadin-server-message` (vaadin/flow#26305) and `vaadin-ui-terminated` (vaadin/flow#27058). Both are in 25.4. Extends the section added in #6566 (merged). ## What changed - The section is renamed to "Watching Requests and Messages from Page Scripts", and its intro lists all three events. - The intro now says that a synthetic `vaadin-ui-terminated` makes the Flow router stop waiting for a pending navigation. Synthetic `vaadin-request` and `vaadin-server-message` events still change nothing in the client. - `vaadin-server-message`: when it fires, the `parsed`/`start`/`end` events of the message, its `request` and `outcome` properties, the cases where events are skipped, and an example that measures handling time. - `vaadin-ui-terminated`: when the UI stops, that an embedded web component can start a new UI after "Session expired", and `isTerminated()`. - The paragraph on dispatching synthetic events now covers messages too. ## Test summary - Checked every statement against the Flow client sources (`ClientEvents`, `VaadinServerMessage`, `UILifecycle`, `MessageHandler`, and the XHR and push connections) on `main` and `25.4`. Working on behalf of Petter --------- Co-authored-by: Vaadin Bot <vaadin-bot@users.noreply.github.com> Co-authored-by: totally-not-ai[bot] <290682512+totally-not-ai[bot]@users.noreply.github.com>
Documentation for vaadin/flow#26064 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
articles/flow/advanced/server-client-communication.adocwindow.Vaadin.Flow.clients[appId].events, thevaadin-requestevent, and thesent/error/response/endlifecycle of the request object it carries, including theattempt,failure, andoutcomeproperties and the ability to dispatch synthetic events for testing.Auto-generated by the Documentation Bot — review before merging.