Skip to content
23 changes: 20 additions & 3 deletions articles/flow/advanced/server-client-communication.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ This process ensures UI consistency, even in the presence of errors or unexpecte


[role="since:com.vaadin:vaadin@V25.4"]
== Watching Requests from Page Scripts
== Watching Requests and Messages from Page Scripts

`window.Vaadin.Flow.clients[appId].events` is an `EventTarget` that announces the requests the client sends to the server, so a page script can follow one, for example to measure it. Heartbeats aren't announced. The client only dispatches on it and never listens to it, so nothing a page script dispatches on it changes what the client does.
`window.Vaadin.Flow.clients[appId].events` is an `EventTarget` that announces the requests the client sends to the server, the messages it receives from the server, and the UI stopping. A page script can follow a request or a message, for example to measure it. Heartbeats aren't announced. The client engine dispatches on it but doesn't listen to it, so a synthetic `vaadin-request` or `vaadin-server-message` that a page script dispatches changes nothing about what the client does. A synthetic `vaadin-ui-terminated` does have an effect: the Flow router listens to it to stop waiting for a pending navigation.

`window.Vaadin.Flow.clients` holds one client for each Vaadin application on the page, keyed by its application ID. A page that runs a single application can pick the client that has `events`, as the example below does. The client is added once the application has started, so run the script after that, for example from a view through [methodname]`Page.executeJs()`.

Expand All @@ -105,7 +105,24 @@ client.events.addEventListener('vaadin-request', ({ detail: request }) => {
});
----

`events` is a full `EventTarget`, so a page script can also dispatch events on it, such as a synthetic `vaadin-request`, to unit test its own listeners without a real request. The request's properties are read-only, and dispatching an event on a request from outside the client reaches only its listeners -- it changes nothing about the request or the client.
A `vaadin-server-message` event is dispatched when data arrives from the server, before the client parses it. It covers both replies to requests and messages that the server pushes on its own. Its `detail` is the message, which then dispatches events of its own as the client handles it:

`parsed`:: The message was parsed and identified. The message's `request` property is the request it replies to. It's `undefined` if the server sent the message on its own, or if the message repeats a reply to a request that the client no longer knows.
`start`:: The client starts handling the message. Messages are handled in order, and this happens before the client loads the dependencies that the message brings.
`end`:: The message's `outcome` property says how it was handled: `applied`, `discarded` if the client dropped it without handling it, or `failed` if handling it threw an error, so it may be applied only in part. `end` is the last event. A message that couldn't be parsed dispatches `end` without `parsed` or `start`. A message that the client never gets to handle, such as one waiting for a dependency that never loads, doesn't dispatch `end`.

[source,javascript]
----
client.events.addEventListener('vaadin-server-message', ({ detail: message }) => {
const arrivedAt = performance.now();
message.addEventListener('end', () =>
console.log(`message ${message.outcome} after ${performance.now() - arrivedAt} ms`));
});
----

A `vaadin-ui-terminated` event is dispatched when the UI stops, for example after an "Internal error" or "Session expired" reply, or after a communication error that the client doesn't recover from. A page script that waits for a reply from the server can stop waiting, since the stopped UI doesn't answer anymore. The stop isn't always final: an embedded web component resynchronizes the session after "Session expired" and runs a new UI. The client's `isTerminated()` method tells whether the UI is stopped at a given moment.

`events` is a full `EventTarget`, so a page script can also dispatch events on it, such as a synthetic `vaadin-request` or `vaadin-server-message`, to unit test its own listeners without a real request or message. The properties of a request or a message are read-only. Dispatching an event on one from outside the client reaches only its listeners -- it changes nothing about the request, the message, or the client.


[role="since:com.vaadin:vaadin@V25.2"]
Expand Down
Loading