tauri_plugin_centrifugo/
lib.rs

1//! # Tauri Plugin Centrifugo
2//!
3//! A Tauri plugin for real-time communication with Centrifugo server using the `tokio-centrifuge` library.
4//!
5//! ## Features
6//!
7//! - **Real-time Communication**: WebSocket-based communication with Centrifugo
8//! - **JSON & Protobuf Support**: Choose between JSON and Protobuf protocols
9//! - **Event-driven Architecture**: Comprehensive event system for connection state and messages
10//! - **Channel Management**: Subscribe/unsubscribe to channels dynamically
11//! - **Authentication**: JWT token support for secure connections
12//! - **Cross-platform**: Works on desktop and mobile platforms
13//!
14//! ## Usage
15//!
16//! ```rust,no_run
17//! use tauri_plugin_centrifugo::init;
18//!
19//! // fn main() {
20//! //     tauri::Builder::default()
21//! //         .plugin(init())
22//! //         .run(tauri::generate_context!())
23//! //         .expect("error while running tauri application");
24//! // }
25//! ```
26//!
27//! ## Architecture
28//!
29//! The plugin consists of several modules:
30//! - `commands`: Tauri command handlers for frontend communication
31//! - `core`: Core Centrifugo client logic and state management
32//! - `models`: Data structures for requests, responses, and configuration
33//! - `error`: Custom error types and error handling
34//!
35//! ## Dependencies
36//!
37//! - `tokio-centrifugo`: Rust client library for Centrifugo
38//! - `serde`: Serialization/deserialization
39//! - `thiserror`: Error handling utilities
40//! - `futures-util`: Async stream utilities
41//!
42//! ## License
43//!
44//! MIT License
45
46use tauri::plugin::{Builder as PluginBuilder, TauriPlugin};
47use tauri::{Manager, Runtime};
48
49pub mod commands;
50pub mod core;
51pub mod error;
52pub mod models;
53
54pub use error::Result;
55
56/// Initialize the Centrifugo plugin for Tauri
57///
58/// This function creates and configures the plugin with all necessary command handlers
59/// and state management. It should be called during Tauri app initialization.
60///
61/// # Example
62///
63/// ```rust,no_run
64/// use tauri_plugin_centrifugo::init;
65///
66/// // fn main() {
67/// //     tauri::Builder::default()
68/// //         .plugin(init())
69/// //         .run(tauri::generate_context!())
70/// //         .expect("error while running tauri application");
71/// // }
72/// ```
73///
74/// # Returns
75///
76/// A configured `TauriPlugin<R>` instance that can be added to your Tauri app.
77pub fn init<R: Runtime>() -> TauriPlugin<R> {
78    PluginBuilder::new("centrifugo")
79        .invoke_handler(tauri::generate_handler![
80            commands::connect,
81            commands::disconnect,
82            commands::set_token,
83            commands::publish,
84            commands::rpc,
85            commands::presence,
86            commands::presence_stats,
87            commands::history,
88            commands::send,
89            commands::refresh,
90            commands::sub_refresh,
91            commands::ping,
92            commands::is_connected,
93            commands::get_subscriptions,
94            commands::get_connection_state,
95            commands::add_subscription,
96            commands::remove_subscription,
97        ])
98        .setup(|app, _api| {
99            // Initialize the Centrifugo client state
100            app.manage(core::Centrifugo::new());
101            Ok(())
102        })
103        .build()
104}