arminius_lib_nats/client/sync.rs
1// SPDX-FileCopyrightText: 2025 BWI GmbH
2//
3// SPDX-License-Identifier: Apache-2.0
4
5//! Notifier mechanism to signal something has changed in the system.
6//! Mainly used to notify listeners that changes to a room occurred.
7use async_nats::Subscriber;
8
9use crate::{NatsClient, NatsError};
10
11/// Subject for announcing updates to the sync endpoint
12///
13/// This subject will be used by any instance of either the Client APIs or the
14/// Roomserver to notify all the Client APIs who are awaiting on any `/sync`
15/// update that there might be new data for that synchronization.
16const SYNC_UPDATE_SUBJECT: &str = "sync.update";
17
18impl NatsClient {
19 /// Notifies clients that there is potentially some new data to be
20 /// fetched in the sync endpoint.
21 ///
22 /// NOTE: This is intentionally using a NATS core stream because there is no
23 /// retention/acknowledgment requirements on that stream.
24 pub async fn notify_new_sync_data(&self) -> Result<(), NatsError> {
25 // Here, we could for example just set the message as being the emitter
26 // of the update. But, really, I don't see the need for it for now to be
27 // honest.
28 self.client.publish(SYNC_UPDATE_SUBJECT, "".into()).await?;
29
30 Ok(())
31 }
32
33 /// Subscribes to the sync update subject to get notified if something
34 /// changed.
35 pub async fn sync_data_subscriber(&self) -> Result<Subscriber, NatsError> {
36 Ok(self.client.subscribe(SYNC_UPDATE_SUBJECT).await?)
37 }
38}
39
40#[cfg(test)]
41mod tests;