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
//! # Tauri Commands
//!
//! This module contains all the Tauri command handlers that bridge the frontend
//! JavaScript/TypeScript code with the Rust backend Centrifugo client.
//!
//! ## Command Overview
//!
//! Each command is a `#[tauri::command]` function that:
//! - Receives parameters from the frontend
//! - Accesses the shared Centrifugo client state
//! - Executes the requested operation
//! - Returns results or errors to the frontend
//!
//! ## State Management
//!
//! All commands access the shared `Centrifugo` instance through `window.state::<Centrifugo>()`.
//! This ensures thread-safe access to the client across multiple command invocations.
//!
//! ## Error Handling
//!
//! All commands return `Result<T>` which automatically serializes errors
//! to the frontend with meaningful error messages.
use crateResult;
use crateCentrifugo;
use crate*;
use HashMap;
use ;
/// Establish a connection to Centrifugo server
///
/// Closes a previous client, creates a new one, subscribes to `config.channels`
/// and starts connecting. Returns at once; progress arrives as events, and the
/// SDK reconnects with backoff by itself.
///
/// # Parameters
///
/// - `window`: Tauri window instance for state access
/// - `config`: Connection configuration including URL, channels, and options
///
/// # Returns
///
/// - `Ok(())` once connecting has started
/// - `Err(Error)` on an invalid config (URL, header)
///
/// # Events Emitted
///
/// - `centrifugo:connecting` - Connection attempt started
/// - `centrifugo:connected` - Connection established
/// - `centrifugo:subscribed` - Channel subscriptions successful
/// - `centrifugo:error` - Connection or subscription errors
pub async
/// Close the connection to Centrifugo server
///
/// Closes the connection and drops the client with all its subscriptions.
///
/// # Parameters
///
/// - `window`: Tauri window instance for state access
///
/// # Returns
///
/// - `Ok(())` on successful disconnection
/// - `Err(Error)` if disconnection fails
///
/// # Events Emitted
///
/// - `centrifugo:disconnected` - Connection closed
pub async
/// Set or update the authentication token
///
/// Replaces the connection token; the SDK uses it from the next connect
/// attempt. Fails with `NotConnected` when there is no client.
///
/// # Parameters
///
/// - `window`: Tauri window instance for state access
/// - `token`: New JWT authentication token
///
/// # Returns
///
/// - `Ok(())` on successful token update
/// - `Err(Error)` if token update fails
pub async
/// Answer a pending `centrifugo:need_token` request
///
/// An empty token means "no token": anonymous on connect, unauthorized on
/// refresh. Fails when no request is pending.
pub async
/// Publish a message to a specific channel
///
/// `data` is base64 unless `dataEncoding` is `rawJson`. Map publish
/// (`type`/`key`/`removed`) is rejected with `Not supported`.
///
/// # Parameters
///
/// - `window`: Tauri window instance for state access
/// - `request`: Publish request containing channel and data
///
/// # Returns
///
/// - `Ok(())` on successful publication
/// - `Err(Error)` if publication fails (e.g., not connected)
///
/// # Requirements
///
/// - Must be connected to Centrifugo server
/// - The server must allow client publishing in the channel
pub async
/// Execute an RPC method on the Centrifugo server
///
/// This command sends an RPC request and waits for the response.
/// The request data should be base64-encoded.
///
/// # Parameters
///
/// - `window`: Tauri window instance for state access
/// - `request`: RPC request containing method name and parameters
///
/// # Returns
///
/// - `Ok(String)` containing the base64-encoded response data
/// - `Err(Error)` if RPC execution fails
///
/// # Requirements
///
/// - Must be connected to Centrifugo server
pub async
/// Get presence information for a channel
///
/// Returns the client IDs currently present in the channel.
///
/// # Parameters
///
/// - `window`: Tauri window instance for state access
/// - `request`: Presence request containing channel name
///
/// # Returns
///
/// - `Ok(Vec<String>)` containing client identifiers
/// - `Err(Error)` if the request fails
pub async
/// Get presence statistics for a channel
///
/// Returns aggregated statistics about the number of clients and users
/// in a channel.
///
/// # Parameters
///
/// - `window`: Tauri window instance for state access
/// - `request`: Presence stats request containing channel name
///
/// # Returns
///
/// - `Ok((u32, u32))` containing (num_clients, num_users)
/// - `Err(Error)` if the request fails
pub async
/// Get message history for a channel
///
/// Retrieves publications from the channel history (`limit`, `reverse`;
/// `since` is ignored). The channel must have history enabled.
///
/// # Parameters
///
/// - `window`: Tauri window instance for state access
/// - `request`: History request with channel and filtering options
///
/// # Returns
///
/// - `Ok(Vec<PublicationData>)` containing historical publications
/// - `Err(Error)` if the request fails
pub async
/// Send a message directly to the server
///
/// Sends an async message to the server (not to a channel, no reply).
///
/// # Parameters
///
/// - `window`: Tauri window instance for state access
/// - `request`: Send request containing message data
///
/// # Returns
///
/// - `Ok(())` on successful send
/// - `Err(Error)` if sending fails
pub async
/// Answer a pending `need_token` request
///
/// Answers a pending `centrifugo:need_token` request with the token; when none
/// is pending, sets the token for the next connect. The SDK refreshes tokens
/// itself before they expire.
///
/// # Parameters
///
/// - `window`: Tauri window instance for state access
/// - `request`: Refresh request containing new token
///
/// # Returns
///
/// - `Ok(())` on successful token refresh
/// - `Err(Error)` if refresh fails
pub async
/// Answer a pending `need_sub_token` request for a channel
///
/// Answers a pending `centrifugo:need_sub_token` request for `request.channel`.
/// Fails when none is pending. Track/untrack fields are rejected with
/// `Not supported`; `signature` is ignored.
///
/// # Parameters
///
/// - `window`: Tauri window instance for state access
/// - `request`: Sub refresh request containing channel and new token
///
/// # Returns
///
/// - `Ok(())` on successful token refresh
/// - `Err(Error)` if refresh fails
pub async
/// Deprecated: check that the client is connected
///
/// The SDK keeps the connection alive with server pings and has no client
/// ping; this only fails with `NotConnected` when not connected.
///
/// # Parameters
///
/// - `window`: Tauri window instance for state access
///
/// # Returns
///
/// - `Ok(())` when connected
/// - `Err(Error::NotConnected)` otherwise
pub async
/// Check if currently connected to Centrifugo server
///
/// Returns the current connection status without establishing a connection.
///
/// # Parameters
///
/// - `window`: Tauri window instance for state access
///
/// # Returns
///
/// - `Ok(bool)` - `true` if connected, `false` otherwise
/// - `Err(Error)` if status check fails
pub async
/// Get current channel subscriptions
///
/// Returns the channels added through the plugin; the value is `true` while
/// subscribed.
///
/// # Parameters
///
/// - `window`: Tauri window instance for state access
///
/// # Returns
///
/// - `Ok(HashMap<String, bool>)` - Channel name to subscription status mapping
/// - `Err(Error)` if subscription retrieval fails
pub async
/// Get detailed connection state
///
/// One of `connecting`, `connected`, `closed`, `disconnected` (also when there
/// is no client).
///
/// # Parameters
///
/// - `window`: Tauri window instance for state access
///
/// # Returns
///
/// - `Ok(String)` - Connection state description
/// - `Err(Error)` if state retrieval fails
pub async
/// Add a new channel subscription
///
/// Subscribes to a channel with default options. Fails with `NotConnected`
/// when there is no client.
///
/// # Parameters
///
/// - `window`: Tauri window instance for state access
/// - `channel`: Channel name to subscribe to
///
/// # Returns
///
/// - `Ok(())` on successful subscription
/// - `Err(Error)` if subscription fails
///
/// # Events Emitted
///
/// - `centrifugo:subscribing` - Subscription attempt started
/// - `centrifugo:subscribed` - Subscription successful
/// - `centrifugo:error` - Subscription errors
pub async
/// Add a channel subscription with options
///
/// Token (private channel, refreshed via `centrifugo:need_sub_token`),
/// recovery, join/leave, positioning, fossil delta, resume position.
///
/// # Events Emitted
///
/// - `centrifugo:subscribing`, `centrifugo:subscribed`, `centrifugo:error`
pub async
/// Remove a channel subscription
///
/// Unsubscribes from a channel and forgets it. Unknown channels are ignored.
///
/// # Parameters
///
/// - `window`: Tauri window instance for state access
/// - `channel`: Channel name to unsubscribe from
///
/// # Returns
///
/// - `Ok(())` on successful unsubscription
/// - `Err(Error)` if unsubscription fails
///
/// # Events Emitted
///
/// - `centrifugo:unsubscribed` - Unsubscription successful
pub async
/// Remove every channel subscription
pub async