RPC Capabilities
Relevant source files
The following files were used as context for generating this wiki page:
The RPC (Remote Procedure Call) system in the Tauri Centrifugo plugin provides a structured mechanism for bi-directional communication between the Tauri client and the Centrifugo server. Unlike the standard pub/sub model where messages are broadcast to channels, RPC allows for point-to-point requests where a client executes a specific method on the server and waits for a corresponding response.
This capability is essential for operations requiring immediate feedback, such as retrieving user profiles, validating actions, or querying specific server-side states. The plugin handles the serialization of request parameters, the asynchronous execution of the call via the Rust backend, and the delivery of the response back to the frontend.
Sources: src/lib.rs:22-26, README.md:95-103
Architecture and Data Flow
The RPC system operates as a bridge between the JavaScript frontend, the Rust plugin core, and the remote Centrifugo server. The flow starts with a frontend call that is dispatched to a Rust command, which then uses the underlying tokio-centrifuge library to communicate with the server.
The following sequence diagram illustrates the lifecycle of an RPC call:
Sources: src/commands.rs:118-132, README.md:95-103
Data Structures
The RPC system relies on specific request and response models to ensure type safety and consistent serialization across the IPC (Inter-Process Communication) boundary.
RPC Request Models
| Field | Type | Description |
|---|---|---|
method | string | The name of the remote method to be executed on the server. |
data | string | Base64-encoded parameters for the method call. |
Sources: src/models.rs:88-95, examples/server/proto/api.proto:246-249
RPC Response Models
| Field | Type | Description |
|---|---|---|
method | string | The name of the method that was executed. |
data | string | Base64-encoded response data returned by the server. |
timestamp | i64 | Unix timestamp of when the response was generated. |
Sources: src/models.rs:198-207, examples/server/proto/api.proto:251-258
Implementation Details
Backend Command Handling
The rpc command is registered in the plugin initialization and acts as the primary entry point for the frontend. It requires an active connection to the Centrifugo server to function.
#[tauri::command]
pub async fn rpc<R: Runtime>(window: Window<R>, request: RpcRequest) -> Result<String> {
let centrifugo = window.state::<Centrifugo>();
centrifugo.rpc(request).await
}
Sources: src/commands.rs:129-132, src/lib.rs:82
Frontend Helper Utilities
The plugin provides helpers.rpcJson to simplify the process of sending JSON objects. This helper automatically handles the base64 encoding of the input and the decoding of the server's response.
// Execute RPC with JSON data
const result = await helpers.rpcJson('method-name', {
userId: 123,
action: 'getInfo'
})
Sources: README.md:98-102, examples/tauri-app/src/App.svelte:315-325
Server-Side Definition (gRPC/Protobuf)
On the server side, RPC calls are defined within the CentrifugoApi service. The server processes the RPCRequest containing the method and params, then returns an RPCResponse.
service CentrifugoApi {
rpc RPC (RPCRequest) returns (RPCResponse) {}
}
message RPCRequest {
string method = 1;
bytes params = 2;
}
message RPCResponse {
Error error = 1;
RPCResult result = 2;
}
Sources: examples/server/proto/api.proto:15, examples/server/proto/api.proto:246-254
Summary of RPC Capabilities
| Feature | Description |
|---|---|
| Synchronous Execution | Waits for server confirmation and result before resolving the promise. |
| Base64 Encoding | Ensures binary-safe transmission of data over the WebSocket and IPC. |
| JSON Helpers | Built-in support for automatic serialization of JavaScript objects. |
| Error Handling | Server-side errors are propagated back to the client as rejected promises. |
Sources: src/commands.rs:118-132, README.md:95-103