tauri-plugin-centrifugo 0.2.2

Tauri plugin for Centrifugo real-time communication built on the official centrifuge-client SDK
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//! # Core Centrifugo Client
//!
//! State manager for the plugin, built on the official
//! [`centrifuge-client`](https://github.com/centrifugal/centrifuge-rust) SDK.
//! All connection and channel events are forwarded to the frontend as Tauri
//! events with the `centrifugo:` prefix.

use crate::error::{Error, Result};
use crate::models::*;
use base64::Engine;
use base64::engine::general_purpose::STANDARD as BASE64;
use centrifuge::{
    Client, ClientEvent, ClientInfo, DeltaType, HistoryOptions, Protocol, Publication, State,
    StreamPosition, SubState, Subscription, SubscriptionEvent, TokenError, TransportEndpoint,
};
use futures_util::StreamExt;
use serde_json::{Value, json};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use tauri::{AppHandle, Emitter, Runtime};
use tokio::sync::{RwLock, oneshot};

type Waiters = Arc<RwLock<HashMap<String, oneshot::Sender<String>>>>;

/// Key of the connection token waiter in [`Centrifugo::token_waiters`].
const CONNECTION: &str = "";

fn decode_payload(data: &str, encoding: Option<DataEncoding>) -> Result<Vec<u8>> {
    match encoding.unwrap_or(DataEncoding::Base64) {
        DataEncoding::Base64 => Ok(BASE64.decode(data)?),
        DataEncoding::RawJson => {
            let _: Value = serde_json::from_str(data)?;
            Ok(data.as_bytes().to_vec())
        }
    }
}

fn now() -> i64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |d| d.as_millis() as i64)
}

fn info_json(info: &ClientInfo) -> ClientInfoData {
    ClientInfoData {
        client: info.client.clone(),
        user: info.user.clone(),
        conn_info: BASE64.encode(info.conn_info.as_deref().unwrap_or_default()),
        chan_info: BASE64.encode(info.chan_info.as_deref().unwrap_or_default()),
    }
}

fn publication_data(channel: &str, p: &Publication) -> PublicationData {
    PublicationData {
        channel: p.channel.clone().unwrap_or_else(|| channel.to_string()),
        data: BASE64.encode(&p.data),
        data_encoding: Some(DataEncoding::Base64),
        raw_json: serde_json::from_slice::<Value>(&p.data).ok(),
        offset: Some(p.offset),
        info: p.info.as_ref().map(info_json),
        tags: Some(p.tags.clone()),
        timestamp: p.time.unwrap_or_else(now),
    }
}

/// Asks the frontend for a token (`centrifugo:<event>`) and waits for
/// `provide_token` / `sub_refresh` to answer under `key`. No timeout, as in
/// centrifuge-js: the SDK ignores the result if `disconnect()` ends the
/// attempt. An empty answer means "no token" (anonymous on connect,
/// unauthorized on refresh / for a subscription).
async fn request_token<R: Runtime>(
    app: AppHandle<R>,
    waiters: Waiters,
    key: String,
    event: &'static str,
) -> std::result::Result<String, TokenError> {
    let (tx, rx) = oneshot::channel();
    waiters.write().await.insert(key.clone(), tx);
    let payload = if key.is_empty() {
        json!({ "ts": now() })
    } else {
        json!({ "channel": key, "ts": now() })
    };
    let _ = app.emit(event, payload);
    // The sender is dropped when a newer request replaces this one or on
    // disconnect: retry (with backoff), never treat it as "unauthorized".
    rx.await
        .map_err(|_| TokenError::Other("token request superseded".into()))
}

/// TLS for Android without the platform verifier's JNI setup: Mozilla's
/// roots. ponytail: user-installed CAs are not trusted; pass the SDK's
/// `android::init_with_env` path if an app needs them.
#[cfg(target_os = "android")]
fn android_tls() -> centrifuge::TlsConfig {
    let roots = rustls::RootCertStore {
        roots: webpki_roots::TLS_SERVER_ROOTS.to_vec(),
    };
    let cfg = rustls::ClientConfig::builder()
        .with_root_certificates(roots)
        .with_no_client_auth();
    centrifuge::TlsConfig::rustls_client_config(cfg)
}

/// Main Centrifugo state managed by Tauri.
pub struct Centrifugo {
    client: Arc<RwLock<Option<Client>>>,
    token_waiters: Waiters,
}

impl Centrifugo {
    pub fn new() -> Self {
        Self {
            client: Arc::new(RwLock::new(None)),
            token_waiters: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    async fn client(&self) -> Result<Client> {
        self.client.read().await.clone().ok_or(Error::NotConnected)
    }

    /// Creates the client, subscribes to `config.channels` and starts
    /// connecting. Returns at once; progress arrives as events. On an invalid
    /// config the previous client is kept.
    pub async fn connect<R: Runtime>(&self, app: AppHandle<R>, config: StartConfig) -> Result<()> {
        let mut endpoints = vec![TransportEndpoint::websocket(config.url)];
        if let Some(url) = config.http_stream_url {
            endpoints.push(TransportEndpoint::http_stream(url));
        }
        let mut builder = Client::builder_with_transports(endpoints)
            .runtime(tauri::async_runtime::handle().inner().clone())
            .protocol(if config.use_protobuf {
                Protocol::Protobuf
            } else {
                Protocol::Json
            });
        #[cfg(target_os = "android")]
        {
            builder = builder.tls(android_tls());
        }
        // The SDK calls `get_token` when the token is empty, and before it
        // expires. Install it only when the app uses tokens, so anonymous
        // connections never wait for the frontend.
        let request_token_cb = config.token.is_some() || config.request_token.unwrap_or(false);
        if let Some(token) = config.token {
            builder = builder.token(token);
        }
        if let Some(name) = config.name {
            builder = builder.name(name);
        }
        if let Some(version) = config.version {
            builder = builder.version(version);
        }
        if let Some(headers) = config.headers {
            let mut map = http::HeaderMap::new();
            for (k, v) in headers {
                let name = http::HeaderName::try_from(k.as_str())
                    .map_err(|e| Error::RequestError(format!("header {k}: {e}")))?;
                let value = http::HeaderValue::try_from(v.as_str())
                    .map_err(|e| Error::RequestError(format!("header {k}: {e}")))?;
                map.insert(name, value);
            }
            builder = builder.http_headers(map);
        }
        if let Some(ms) = config.read_timeout {
            builder = builder.timeout(Duration::from_millis(ms));
        }
        if request_token_cb {
            let (token_app, waiters) = (app.clone(), self.token_waiters.clone());
            builder = builder.get_token(move |_| {
                let fut = request_token(
                    token_app.clone(),
                    waiters.clone(),
                    CONNECTION.into(),
                    "centrifugo:need_token",
                );
                async move { fut.await.map(Some) }
            });
        }
        let client = builder.build()?;

        // A channel listed twice is subscribed once.
        let mut channels = config.channels;
        channels.sort();
        channels.dedup();
        for channel in channels {
            let _ = self.subscribe(&app, &client, SubscribeOptions::channel(channel))?;
        }

        let old = self.client.write().await.take();
        if let Some(old) = old {
            old.close().await;
        }
        forward_client_events(app.clone(), &client);
        client.connect()?;
        *self.client.write().await = Some(client);
        Ok(())
    }

    fn subscribe<R: Runtime>(
        &self,
        app: &AppHandle<R>,
        client: &Client,
        opts: SubscribeOptions,
    ) -> Result<Subscription> {
        let mut builder = client
            .new_subscription(opts.channel.clone())
            .recoverable(opts.recoverable.unwrap_or(false))
            .join_leave(opts.join_leave.unwrap_or(false))
            .positioned(opts.positioned.unwrap_or(false));
        if opts.delta.unwrap_or(false) {
            builder = builder.delta(DeltaType::Fossil);
        }
        if let Some(pos) = opts.since {
            builder = builder.since(StreamPosition::new(pos.offset, pos.epoch));
        }
        if let Some(token) = opts.token {
            // Private channel: when the token expires, ask the frontend for a
            // new one (`centrifugo:need_sub_token`, answered by `sub_refresh`).
            let (token_app, waiters) = (app.clone(), self.token_waiters.clone());
            builder = builder.token(token).get_token(move |ctx| {
                request_token(
                    token_app.clone(),
                    waiters.clone(),
                    ctx.channel,
                    "centrifugo:need_sub_token",
                )
            });
        }
        let sub = builder.build()?;
        forward_subscription_events(app.clone(), &sub);
        sub.subscribe();
        Ok(sub)
    }

    pub async fn disconnect(&self) -> Result<()> {
        let client = self.client.write().await.take();
        if let Some(client) = client {
            client.close().await;
        }
        self.token_waiters.write().await.clear();
        Ok(())
    }

    /// Replaces the connection token used from the next connect attempt.
    pub async fn set_token(&self, token: String) -> Result<()> {
        self.client().await?.set_token(token);
        Ok(())
    }

    pub async fn publish(&self, request: PublishRequest) -> Result<()> {
        if request.r#type.unwrap_or(0) != 0
            || request.key.as_deref().is_some_and(|k| !k.is_empty())
            || request.removed.unwrap_or(false)
        {
            return Err(Error::Unsupported("map publish (type/key/removed)"));
        }
        let data = decode_payload(&request.data, request.data_encoding)?;
        Ok(self.client().await?.publish(&request.channel, data).await?)
    }

    pub async fn is_connected(&self) -> Result<bool> {
        Ok(self
            .client
            .read()
            .await
            .as_ref()
            .is_some_and(|c| c.state() == State::Connected))
    }

    /// Channels added through this plugin; `true` when currently subscribed.
    pub async fn get_subscriptions(&self) -> Result<HashMap<String, bool>> {
        Ok(match self.client.read().await.as_ref() {
            Some(c) => c
                .subscriptions()
                .into_iter()
                .map(|(ch, s)| (ch, s.state() == SubState::Subscribed))
                .collect(),
            None => HashMap::new(),
        })
    }

    pub async fn get_connection_state(&self) -> Result<String> {
        let state = self.client.read().await.as_ref().map(Client::state);
        Ok(match state {
            Some(State::Connecting) => "connecting",
            Some(State::Connected) => "connected",
            Some(State::Closed) => "closed",
            _ => "disconnected",
        }
        .to_string())
    }

    pub async fn add_subscription<R: Runtime>(
        &self,
        app: AppHandle<R>,
        channel: String,
    ) -> Result<()> {
        self.add_subscription_with_options(app, SubscribeOptions::channel(channel))
            .await
    }

    pub async fn add_subscription_with_options<R: Runtime>(
        &self,
        app: AppHandle<R>,
        opts: SubscribeOptions,
    ) -> Result<()> {
        let client = self.client().await?;
        self.subscribe(&app, &client, opts)?;
        Ok(())
    }

    pub async fn remove_subscription<R: Runtime>(
        &self,
        _app: AppHandle<R>,
        channel: String,
    ) -> Result<()> {
        let client = self.client().await?;
        if let Some(sub) = client.get_subscription(&channel) {
            sub.unsubscribe();
            client.remove_subscription(&sub)?;
        }
        self.token_waiters.write().await.remove(&channel);
        Ok(())
    }

    pub async fn remove_all_subscriptions<R: Runtime>(&self, app: AppHandle<R>) -> Result<()> {
        let channels: Vec<String> = self.client().await?.subscriptions().into_keys().collect();
        for channel in channels {
            self.remove_subscription(app.clone(), channel).await?;
        }
        Ok(())
    }

    pub async fn rpc(&self, request: RpcRequest) -> Result<String> {
        let data = decode_payload(&request.data, request.data_encoding)?;
        let result = self.client().await?.rpc(&request.method, data).await?;
        Ok(BASE64.encode(&result.data))
    }

    pub async fn presence(&self, request: PresenceRequest) -> Result<Vec<String>> {
        let result = self.client().await?.presence(&request.channel).await?;
        Ok(result.clients.into_keys().collect())
    }

    pub async fn presence_stats(&self, request: PresenceStatsRequest) -> Result<(u32, u32)> {
        let result = self
            .client()
            .await?
            .presence_stats(&request.channel)
            .await?;
        Ok((result.num_clients, result.num_users))
    }

    pub async fn history(&self, request: HistoryRequest) -> Result<Vec<PublicationData>> {
        let mut opts = HistoryOptions::new().reverse(request.reverse.unwrap_or(false));
        if let Some(limit) = request.limit {
            opts = opts.limit(limit);
        }
        let result = self.client().await?.history(&request.channel, opts).await?;
        Ok(result
            .publications
            .iter()
            .map(|p| publication_data(&request.channel, p))
            .collect())
    }

    pub async fn send(&self, request: SendRequest) -> Result<()> {
        let data = decode_payload(&request.data, request.data_encoding)?;
        Ok(self.client().await?.send(data).await?)
    }

    /// Answers a pending `need_token` request, or sets the token for the next
    /// connect. The SDK refreshes connection tokens itself via `need_token`.
    pub async fn refresh(&self, request: RefreshRequest) -> Result<()> {
        let client = self.client().await?;
        if let Some(waiter) = self.token_waiters.write().await.remove(CONNECTION) {
            let _ = waiter.send(request.token);
        } else {
            client.set_token(request.token);
        }
        Ok(())
    }

    /// Answers a pending `need_sub_token` request for the channel.
    pub async fn sub_refresh(&self, request: SubRefreshRequest) -> Result<()> {
        if request.r#type.unwrap_or(0) != 0
            || request.items.is_some()
            || request.untrack_keys.is_some()
        {
            return Err(Error::Unsupported("track/untrack sub refresh"));
        }
        self.client().await?;
        let waiter = self.token_waiters.write().await.remove(&request.channel);
        let waiter = waiter.ok_or_else(|| {
            Error::RequestError(format!(
                "No token request in progress for {}",
                request.channel
            ))
        })?;
        let _ = waiter.send(request.token);
        Ok(())
    }

    /// The SDK keeps the connection alive with server pings; this only checks
    /// that the client is connected.
    pub async fn ping(&self) -> Result<()> {
        if self.is_connected().await? {
            Ok(())
        } else {
            Err(Error::NotConnected)
        }
    }

    pub async fn provide_token(&self, token: String) -> Result<()> {
        let waiter = self.token_waiters.write().await.remove(CONNECTION);
        let waiter =
            waiter.ok_or_else(|| Error::RequestError("No token request in progress".into()))?;
        let _ = waiter.send(token);
        Ok(())
    }
}

impl Default for Centrifugo {
    fn default() -> Self {
        Self::new()
    }
}

fn forward_client_events<R: Runtime>(app: AppHandle<R>, client: &Client) {
    let mut events = client.events();
    tauri::async_runtime::spawn(async move {
        while let Some(event) = events.next().await {
            let (name, payload) = match event {
                ClientEvent::Connecting(e) => (
                    "connecting",
                    json!({"ts": now(), "code": e.code, "reason": e.reason}),
                ),
                ClientEvent::Connected(e) => {
                    ("connected", json!({"ts": now(), "client": e.client_id}))
                }
                ClientEvent::Disconnected(e) => (
                    "disconnected",
                    json!({"ts": now(), "code": e.code, "reason": e.reason}),
                ),
                ClientEvent::Error(e) => ("error", json!({"error": e.error.to_string()})),
                ClientEvent::ServerSubscribed(e) => {
                    ("server_subscribed", json!({"channel": e.channel}))
                }
                ClientEvent::ServerSubscribing(e) => {
                    ("server_subscribing", json!({"channel": e.channel}))
                }
                ClientEvent::ServerUnsubscribed(e) => {
                    ("server_unsubscribed", json!({"channel": e.channel}))
                }
                ClientEvent::ServerPublication(e) => (
                    "server_publication",
                    json!(publication_data(&e.channel, &e.publication)),
                ),
                ClientEvent::ServerJoin(e) => (
                    "server_join",
                    json!({"channel": e.channel, "client": e.info.client, "user": e.info.user}),
                ),
                ClientEvent::ServerLeave(e) => (
                    "server_leave",
                    json!({"channel": e.channel, "client": e.info.client, "user": e.info.user}),
                ),
                _ => continue,
            };
            let _ = app.emit(&format!("centrifugo:{name}"), payload);
        }
    });
}

fn forward_subscription_events<R: Runtime>(app: AppHandle<R>, sub: &Subscription) {
    let channel = sub.channel().to_string();
    let mut events = sub.events();
    tauri::async_runtime::spawn(async move {
        while let Some(event) = events.next().await {
            let (name, payload) = match event {
                SubscriptionEvent::Subscribing(e) => (
                    "subscribing",
                    json!({"channel": channel, "code": e.code, "reason": e.reason}),
                ),
                SubscriptionEvent::Subscribed(e) => {
                    let pos = e.stream_position.map(|p| StreamPositionData {
                        offset: p.offset,
                        epoch: p.epoch,
                    });
                    let payload = json!({
                        "channel": channel,
                        "wasRecovering": e.was_recovering,
                        "recovered": e.recovered,
                        "streamPosition": pos,
                    });
                    ("subscribed", payload)
                }
                SubscriptionEvent::Unsubscribed(e) => (
                    "unsubscribed",
                    json!({"channel": channel, "code": e.code, "reason": e.reason}),
                ),
                SubscriptionEvent::Publication(e) => (
                    "publication",
                    json!(publication_data(&channel, &e.publication)),
                ),
                SubscriptionEvent::Join(e) => (
                    "join",
                    json!({"channel": channel, "info": info_json(&e.info)}),
                ),
                SubscriptionEvent::Leave(e) => (
                    "leave",
                    json!({"channel": channel, "info": info_json(&e.info)}),
                ),
                SubscriptionEvent::Error(e) => (
                    "error",
                    json!({"channel": channel, "error": e.error.to_string()}),
                ),
                _ => continue,
            };
            let _ = app.emit(&format!("centrifugo:{name}"), payload);
        }
    });
}