Skip to main content

centrifuge_client/
lib.rs

1//! # centrifuge-client
2//!
3//! Rust client SDK for the [Centrifuge](https://centrifugal.dev/) real-time messaging protocol.
4//!
5//! Connect to [Centrifugo](https://github.com/centrifugal/centrifugo) or any Centrifuge-based
6//! server over WebSocket, subscribe to channels, and receive publications in real time.
7//!
8//! ## Quick Start
9//!
10//! ```no_run
11//! use centrifuge_client::{Client, ClientConfig, SubEvent};
12//!
13//! # #[tokio::main]
14//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
15//! let client = Client::new(ClientConfig::new("ws://localhost:8000/connection/websocket"));
16//!
17//! let (sub, mut events) = client.subscribe("chat").await?;
18//! client.connect().await?;
19//!
20//! while let Some(event) = events.recv().await {
21//!     match event {
22//!         SubEvent::Publication(pub_data) => println!("{} bytes", pub_data.data.len()),
23//!         SubEvent::Subscribed(ctx) => println!("subscribed to {}", ctx.channel),
24//!         _ => {}
25//!     }
26//! }
27//! # Ok(())
28//! # }
29//! ```
30//!
31//! The main entry points are [`Client`] and [`Subscription`]. See [`ClientConfig`] and
32//! [`SubscriptionConfig`] for configuration options.
33
34#[cfg(not(any(feature = "native-tls", feature = "rustls")))]
35compile_error!("Either the `native-tls` or `rustls` feature must be enabled for TLS support.");
36
37pub(crate) mod backoff;
38pub mod client;
39pub(crate) mod codec;
40pub(crate) mod codes;
41pub mod config;
42pub(crate) mod delta;
43pub mod errors;
44pub(crate) mod protocol;
45pub mod subscription;
46pub mod transport;
47
48// Core types
49pub use client::Client;
50pub use errors::CentrifugeError;
51pub use subscription::Subscription;
52
53// Configuration
54pub use config::{
55    ClientConfig, DeltaType, ProtocolType, SubscriptionConfig, get_data_fn, get_sub_data_fn, get_sub_token_fn,
56    get_token_fn,
57};
58
59// Event types
60pub use protocol::types::{
61    ClientEvent,
62    // Data types
63    ClientInfo,
64    ClientState,
65    // Client event contexts
66    ConnectedContext,
67    ConnectingContext,
68    DisconnectedContext,
69    ErrorContext,
70    // Operation results
71    HistoryOptions,
72    HistoryResult,
73    // Legacy contexts (for direct construction)
74    JoinContext,
75    LeaveContext,
76    MessageContext,
77    PresenceResult,
78    PresenceStatsResult,
79    Publication,
80    PublicationContext,
81    RpcResult,
82    ServerError,
83    ServerJoinContext,
84    ServerLeaveContext,
85    ServerPublicationContext,
86    // Server-side subscription event contexts
87    ServerSubscribedContext,
88    ServerSubscribingContext,
89    ServerUnsubscribedContext,
90    StreamPosition,
91    SubEvent,
92    // Subscription event contexts
93    SubscribedContext,
94    SubscribingContext,
95    SubscriptionState,
96    UnsubscribedContext,
97};
98
99// Protocol
100pub use protocol::proto::FilterNode;
101
102// Compile-time assertions: Client and Subscription must be Send + Sync
103// for safe use across threads in async applications.
104const _: fn() = || {
105    fn assert_send_sync<T: Send + Sync>() {}
106    assert_send_sync::<Client>();
107    assert_send_sync::<Subscription>();
108};