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
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(feature = "test", feature(async_closure))]
//! Hive is a generic Pub/Sub library for Rust.
//! 
//! By default, Hive provides a very basic pub/sub system which you can integrate into something like a web socket server, this system is perfect for small-scale applications which only use one node.
//! 
//! It is important to note that Hive is not for everyone, all the data is returned through one callback which is intended to be piped into things such as websocket servers, mpsc channels, etc. (If you want this functionality, feel free to contact me and I'll put it higher on my priority list)
//! ## Example
//!
//! ```rust
//! use hive_pubsub::{Hive, PubSub};
//! 
//! let mut hive = Hive::new(
//!     |users, data| {
//!         println!("Received data! [{:?}]: {}.", users, data);
//!     }
//! );
//! 
//! hive.subscribe_multiple(1, vec! [ 2, 5 ]).unwrap();
//! hive.subscribe_multiple(3, vec! [ 2, 6 ]).unwrap();
//! hive.subscribe_multiple(4, vec! [ 2, 5 ]).unwrap();
//! 
//! hive.drop_topic(&6).unwrap();
//! hive.publish(&6, "This will not appear.".to_string()).unwrap();
//! hive.publish(&2, "This will appear for all three clients.".to_string()).unwrap();
//! 
//! hive.drop_client(&3).unwrap();
//! hive.publish(&2, "This will appear for two of the clients.".to_string()).unwrap();
//! hive.publish(&5, "This will also appear for two of the clients.".to_string()).unwrap();
//! ```
//!
//! ## Example, for use with MongoDB.
//! 
//! This example is designed for the latest version of MongoDB and is async-runtime agnostic.
//! Tokio is only required for the example below to run, as well as the tests in the library.
//! You may use async-std or tokio.
//!
//! **Important:** before listening on the collection, make sure to create **a capped collection**!
//! My recommendation is that your application should have some sort of database migrations system in place which should automatically create this.
//!
//! ```ignore
//! use std::env::var;
//! use mongodb::Client;
//! use futures::FutureExt;
//! use tokio::sync::mpsc::channel;
//! 
//! use crate::{PubSub};
//! use crate::backend::mongo::{MongodbPubSub, publish};
//! 
//! // You'll want to have some sort of worker which can
//! // handle incoming messages and deal with them accordingly.
//! // Here we are just using a channel with predictable data
//! // so we can just use assert_eq!().
//! let ( sender, mut receiver ) = channel(100);
//! 
//! let client = Client::with_uri_str(&var("MONGODB_URI").unwrap())
//!     .await
//!     .unwrap();
//! 
//! let mut hive = MongodbPubSub::<i32, i32, String>::new(
//!     move |_ids, data| {
//!         let mut s = sender.clone();
//!         // We just send the data into the channel.
//!         tokio::spawn(async move {
//!             s.send(data).await.unwrap();
//!         });
//!     },
//!     client.database("hive").collection("pubsub")
//! );
//! 
//! // We need to subscribe to the topic to get any data.
//! hive.subscribe(0, 0).unwrap();
//! 
//! // Listen to MongoDB collection.
//! let listener = hive.clone();
//! let fut = listener.listen().fuse();
//! 
//! // We are setting source here to make the hive instance
//! // accept incoming data. Since `source` is just a String,
//! // it means we just cloned the original value above and
//! // are now replacing it.
//! hive.set_source("1234".to_string());
//! 
//! let op = async {
//!     // Delay so that the other thread can send a message.
//!     tokio::time::delay_for(tokio::time::Duration::from_secs(2)).await;
//! 
//!     // Hence, we publish our data.
//!     publish(&hive, &0, "The data.".to_string()).await.unwrap();
//! 
//!     // And we should receive it back twice.
//!     assert_eq!(receiver.recv().await.unwrap(), "The data.");
//!     assert_eq!(receiver.recv().await.unwrap(), "The data.");
//! }.fuse();
//! 
//! futures::pin_mut!(fut, op);
//! 
//! futures::select! {
//!     _ = fut => println!("Listener exited early."),
//!     _ = op  => println!("Successfully received data back."),
//! };
//! ```
//!
//! ## Design
//!
//! Hive is designed to be slotted into any server application to act as a middle-man between you and your clients, it will automatically distribute any notifications you give it to all relevant connected clients and other nodes.
//!
//! <p align="center">
//!   <img src="https://gitlab.insrt.uk/insert/hive/-/raw/master/assets/concept.png" />
//! </p>
//!
//! <script>console.log("hive-pubsub, crated by insrt.uk")</script>

mod hive;
mod types;
pub mod backend;

pub use hive::Hive;
pub use types::PubSub;

#[cfg(test)]
mod tests {
    use crate::{Hive, PubSub};

    #[test]
    fn simple() {
        let hive = Hive::new(
            |users, data| {
                println!("We received data! For {:?}, data: {}.", users, data);
            }
        );

        hive.subscribe_multiple("0001".to_string(), vec! [ "0002".to_string(), "0005".to_string() ]).unwrap();
        hive.subscribe_multiple("0003".to_string(), vec! [ "0002".to_string(), "0006".to_string() ]).unwrap();
        hive.subscribe_multiple("0004".to_string(), vec! [ "0002".to_string(), "0005".to_string() ]).unwrap();

        hive.drop_topic(&"0006".to_string()).unwrap();
        hive.publish(&"0006".to_string(), "This will not appear.".to_string()).unwrap();
        hive.publish(&"0002".to_string(), "This will appear for all three clients.".to_string()).unwrap();

        hive.drop_client(&"0003".to_string()).unwrap();
        hive.publish(&"0002".to_string(), "This will appear for two of the clients.".to_string()).unwrap();
        hive.publish(&"0005".to_string(), "This will also appear for two of the clients.".to_string()).unwrap();
    }

    #[test]
    fn generics() {
        let hive: Hive<i32, u32, String> = Hive::new(
            |users, data| {
                println!("We received data! For {:?}, data: {}.", users, data);
            }
        );

        hive.subscribe_multiple(5, vec! [ 2, 3 ]).unwrap();
        hive.publish(&2, "This will appear for one of the clients.".to_string()).unwrap();
    }

    #[tokio::test]
    #[cfg(feature = "mongo-backend")]
    async fn mongo() {
        return;
        dotenv::dotenv().ok();

        use std::env::var;
        use mongodb::Client;
        use futures::FutureExt;
        use tokio::sync::mpsc::channel;

        use crate::{PubSub};
        use crate::backend::mongo::{MongodbPubSub, publish};
        
        // You'll want to have some sort of worker which can
        // handle incoming messages and deal with them accordingly.
        // Here we are just using a channel with predictable data
        // so we can just use assert_eq!().
        let ( sender, mut receiver ) = channel(100);

        let client = Client::with_uri_str(&var("MONGODB_URI")
            .expect("Specify MONGODB_URI in .env or pass in."))
            .await
            .unwrap();

        let mut hive = MongodbPubSub::<i32, i32, String>::new(
            move |_ids, data| {
                let mut s = sender.clone();
                // We just send the data into the channel.
                tokio::spawn(async move {
                    s.send(data).await.unwrap();
                });
            },
            client.database("hive").collection("pubsub")
        );

        // We need to subscribe to the topic to get any data.
        hive.subscribe(0, 0).unwrap();

        // Listen to MongoDB collection.
        let listener = hive.clone();
        let fut = listener.listen().fuse();

        // We are setting source here to make the hive instance
        // accept incoming data. Since `source` is just a String,
        // it means we just cloned the original value above and
        // are now replacing it.
        hive.set_source("1234".to_string());

        let op = async {
            // Delay so that the other thread can send a message.
            tokio::time::delay_for(tokio::time::Duration::from_secs(1)).await;

            // Hence, we publish our data.
            publish(&hive, &0, "The data.".to_string()).await.unwrap();

            // And we should receive it back twice.
            assert_eq!(receiver.recv().await.unwrap(), "The data.");
            assert_eq!(receiver.recv().await.unwrap(), "The data.");
        }.fuse();

        futures::pin_mut!(fut, op);

        futures::select! {
            _ = fut => println!("Listener exited early."),
            _ = op  => println!("Successfully received data back."),
        };
    }

    #[async_std::test]
    #[cfg(feature = "redis-backend")]
    async fn redis() {
        dotenv::dotenv().ok();

        use futures::FutureExt;
        use redis::Commands;
        use tokio::sync::mpsc::channel;

        use crate::{PubSub};
        use crate::backend::redis::{RedisPubSub, publish};
        
        let ( sender, mut receiver ) = channel(100);

        let client = mobc_redis::redis::Client::open("redis://127.0.0.1/").unwrap();
        let manager = mobc_redis::RedisConnectionManager::new(client);
        let pool = mobc::Pool::builder().max_open(100).build(manager);

        let pubsub_con = redis::Client::open("redis://127.0.0.1/").unwrap().get_async_connection().await.unwrap().into_pubsub();

        let mut hive = RedisPubSub::<i32, String, String>::new(
            move |_ids, data| {
                let mut s = sender.clone();
                // We just send the data into the channel.
                tokio::spawn(async move {
                    s.send(data).await.unwrap();
                });
            },
            &pool,
            std::sync::Arc::new(std::sync::Mutex::new(pubsub_con))
        );

        // We need to subscribe to the topic to get any data.
        hive.subscribe(0, "topic".into()).unwrap();

        // Listen to Redis.
        let mut listener = hive.clone();
        let fut = listener.listen().fuse();

        let op = async {
            // Delay so that the other thread can send a message.
            tokio::time::delay_for(tokio::time::Duration::from_secs(1)).await;

            // Hence, we publish our data.
            publish(&hive, "topic".into(), "The data.".to_string()).await.unwrap();

            // And we should receive it back once.
            assert_eq!(receiver.recv().await.unwrap(), "The data.");
        }.fuse();

        futures::pin_mut!(fut, op);

        futures::select! {
            _ = fut => println!("Listener exited early."),
            _ = op  => println!("Successfully received data back."),
        };
    }
}