Rust Data Models
Relevant source files
The following files were used as context for generating this wiki page:
Rust Data Models in the tauri-plugin-centrifugo project define the structured types used for communication between the Tauri Rust backend and the JavaScript frontend. These models facilitate type-safe data exchange, ensuring that configuration, requests, and responses adhere to a consistent schema across the bridge.
The models are primarily located in src/models.rs and are designed to work with Tauri's serialization system. They utilize serde with camelCase naming conventions to align with JavaScript standards while maintaining idiomatic Rust naming conventions internally. Sources: src/models.rs:1-17
Connection and Configuration Models
The plugin uses specific structures to define how a client establishes a connection to the Centrifugo server. The core of this is the StartConfig, which encapsulates the WebSocket URL, authentication details, and initial subscription settings.
The diagram shows the relationship between general connection configuration and specific channel settings. Sources: src/models.rs:20-74
Data Fields Summary
| Model | Field | Type | Description |
|---|---|---|---|
StartConfig | url | String | WebSocket URL for Centrifugo |
StartConfig | channels | Vec<String> | Initial channels to subscribe to |
StartConfig | useProtobuf | bool | Protocol toggle (JSON vs Protobuf) |
ChannelConfig | name | String | The specific channel name |
ChannelStatus | channel | String | Returns the name of a channel status change |
Sources: src/models.rs:37-74, src/models.rs:163-170
Interaction and Request Models
These models represent actions initiated by the frontend, such as publishing messages, executing RPC calls, or requesting historical data. Many of these structures contain a data field that expects base64-encoded content to maintain binary compatibility during transmission. Sources: src/models.rs:76-150
The flow shows how frontend calls are mapped to specific Rust request models before being processed by the core logic. Sources: src/commands.rs:100-150, src/core.rs:315-340
Key Request Structures
PublishRequest: Used to broadcast messages. Requires achanneland base64-encodeddata. Sources: src/models.rs:76-85RpcRequest: Executes remote procedure calls. Includes themethodname and base64dataparameters. Sources: src/models.rs:87-95HistoryRequest: Facilitates message retrieval withlimit,since(pagination), andreverse(ordering) options. Sources: src/models.rs:114-126
Response and Event Models
Responses from the Centrifugo server and internal state changes are mapped to these models before being emitted as events or returned as command results. Sources: src/models.rs:179-254
Server Data Models
| Structure | Description | Key Fields |
|---|---|---|
PublicationData | Data received from a channel | channel, data (base64), timestamp |
RpcResponse | Result of an RPC call | method, data (base64), timestamp |
PresenceResponse | List of active clients | channel, clients (Vec), timestamp |
PresenceStatsResponse | Aggregated channel stats | numClients, numUsers, timestamp |
Sources: src/models.rs:179-232
This diagram illustrates the transformation of raw server messages into the PublicationData model for frontend consumption. Sources: src/core.rs:188-200, src/models.rs:179-190
Internal State Models
The plugin also tracks connection health and subscription status using lightweight structures that provide immediate feedback on the bridge's status.
ConnectionStatus: Provides a Unix timestamp (ts) of when the connection state last changed. Sources: src/models.rs:155-161ErrorData: A standard wrapper for conveying error descriptions from the backend to the UI. Sources: src/models.rs:172-177
Summary
The Rust Data Models in tauri-plugin-centrifugo serve as the contract for the Tauri bridge. By utilizing serde with camelCase transformation, the plugin ensures that Rust's strong typing is maintained on the backend while providing a seamless, idiomatic experience for JavaScript developers on the frontend. Sources: src/models.rs:1-17, src/lib.rs:30-40