Remote Procedure Calls
Remote procedure calls (RPCs) are a way to run procedures or subroutines in a different address space, typically on another machine. Vaadin handles server-client communication by allowing RPC calls from the server to the client — and vice versa.
Calling Client-Side Functions from the Server
Use the Element API to call client-side functions from the server. This is covered in the Calling JavaScript from the Server reference guide.
this.clearSelection() JavaScript function from the server sideSource code
Java
public void clearSelection() {
getElement().callJsFunction("clearSelection");
}Calling Server-Side Methods from the Client
The @ClientCallable annotation allows you to invoke a server-side method from the client side.
Any method in the component class annotated with @ClientCallable can be called from the client side using element.$server.methodName(args), where element is the root element of the component.
You can use it anywhere in your client-side implementation, and you can pass your own arguments to the method. The types should match the method declaration on the server side. The supported argument types are:
-
boolean,int,double, their boxed types (i.e.,Boolean,Integer,Double); -
String; -
JsonValue; and -
enumeration types mapped to strings on the client side
getGreeting(String) method callable from the client sideSource code
Java
@ClientCallable
public String getGreeting(String name) {
return "Hello " + name;
}The client-side method returns a Promise, which is resolved asynchronously with the return value from the server. If the server-side return type is void, the promise is resolved with the return value null.
getGreeting(String) method from a client-side async JavaScript functionSource code
JavaScript
async getServerGreeting() {
let greeting = await this.$server.getGreeting("JavaScript");
console.log(greeting);
}getGreeting(String) method from a client-side JavaScript function using a callbackSource code
JavaScript
getServerGreeting() {
let greetingPromise = this.$server.getGreeting("JavaScript");
greetingPromise.then(greeting => console.log(greeting));
}|
Important
|
Hidden and Disabled Components
If a component is hidden or disabled, client-side method calls to the server are automatically blocked. See the Component Enabled State and Component Visibility reference guides for more information.
|
Server-Side Modality and @ClientCallable
If a component with a @ClientCallable method is underneath a modal dialog or component, it’s considered inert. That means it’s not available for interaction, including RPC calls.
If you want the @ClientCallable method to be available when a component is inert, you’ll need to annotate it with the @AllowInert annotation. Consult the Server-Side Modality reference guide for more information.
getGreeting() even when the component is inertSource code
Java
@ClientCallable
@AllowInert
public String getGreeting(String name) {
return "Hello " + name;
}AB7EDF45-DB22-4560-AF27-FF1DC6944482