Message Publishing
Relevant source files
The following files were used as context for generating this wiki page:
Message Publishing is a core feature of the tauri-plugin-centrifugo project, enabling real-time, asynchronous data transmission from a Tauri application to a Centrifugo server. This mechanism allows clients to broadcast information to specific channels, which is then distributed to all other subscribers of those channels.
The system supports both raw data transmission (base64 encoded) and high-level JSON publishing through helper utilities. It utilizes a bridge between the TypeScript frontend and Rust backend via Tauri's command system, ultimately communicating with the Centrifugo server over a WebSocket connection.
Sources: README.md:12-20, src/lib.rs:7-15
Architecture and Data Flow
The publishing process involves several layers, starting from the frontend API, passing through the Rust command handlers, and reaching the Centrifugo client core.
Publishing Sequence
When a message is published from the frontend, it undergoes serialization and base64 encoding before being passed to the backend.
The sequence above illustrates the flow from the initial publish call in the guest JavaScript to the final server transmission.
Sources: guest-js/index.ts:110-115, src/commands.rs:98-105, src/models.rs:77-85
Core Data Structures
The plugin defines specific structures to handle publishing requests and the resulting data received by other clients (Publications).
PublishRequest
This structure is used by the client to initiate a broadcast.
| Field | Type | Description |
|---|---|---|
channel | string | The target channel name for the message. |
data | string | Base64-encoded content to be broadcast. |
Sources: src/models.rs:77-85, guest-js/index.ts:21-24
PublicationData
When a message is received from a channel, it is encapsulated in this structure.
| Field | Type | Description |
|---|---|---|
channel | string | The source channel from which the message originated. |
data | string | Base64-encoded message content. |
timestamp | i64 | Unix timestamp of when the publication occurred. |
Sources: src/models.rs:188-197, guest-js/index.ts:54-57
Frontend Implementation
The frontend provides two primary ways to publish messages: the low-level publish function and high-level helpers.
Low-level Publishing
The publish function requires manually encoded base64 strings.
import { publish } from 'tauri-plugin-centrifugo-api'
await publish({
channel: 'news',
data: 'SGVsbG8gV29ybGQ=' // "Hello World" in base64
})
Sources: guest-js/index.ts:110-112, README.md:92-95
JSON Helper Publishing
The helpers.publishJson method simplifies the process by automatically handling JSON stringification and base64 encoding via utils.encodeJson.
The helper implementation ensures that complex objects can be sent without manual conversion.
Sources: guest-js/index.ts:262-265, examples/tauri-app/src/App.svelte:316-325
Backend Command Handling
The Rust backend receives the PublishRequest through the publish command. This command retrieves the shared Centrifugo state from the Tauri manager and delegates the operation to the core client.
#[tauri::command]
pub async fn publish<R: Runtime>(window: Window<R>, request: PublishRequest) -> Result<()> {
let centrifugo = window.state::<Centrifugo>();
centrifugo.publish(request).await
}
Sources: src/commands.rs:98-105, src/lib.rs:75-78
Protocol Definition
At the server level, as defined in the Protobuf specifications, publishing involves a PublishRequest containing channel information and raw bytes or base64 data.
| Field | Tag | Type | Description |
|---|---|---|---|
channel | 1 | string | Destination channel. |
data | 2 | bytes | Raw binary data. |
b64data | 3 | string | Base64 representation of data. |
skip_history | 4 | bool | Flag to prevent saving to history. |
Sources: examples/server/proto/api.proto:115-122
Summary
Message Publishing in the Tauri Centrifugo plugin provides a multi-layered approach to real-time broadcasting. It leverages TypeScript helpers for ease of use in the UI, utilizes base64 encoding for safe transport through Tauri's IPC bridge, and executes asynchronous Rust commands to interact with the Centrifugo server. The system supports both JSON and Protobuf protocols, ensuring flexibility for different application requirements.