1
- //! # tagged-channel
1
+ //! # tagged-channels
2
2
//!
3
- //! SSE channels manager for Axum framework
3
+ //! This library makes it easy to tag (WebSocket, SSE, ...) channels with e.g. user-id and then
4
+ //! send events to all the channels opened by a particular user. It's framework agnostic, but for
5
+ //! now has only an [axum example]. If you're using it with another framework, consider PR-ing an
6
+ //! adapted example.
4
7
//!
5
8
//! ## Usage
6
9
//!
7
10
//! ```rust,no_run
8
11
//! # use serde::{Deserialize, Serialize};
9
- //! # use tagged_channels::SseManager ;
12
+ //! # use tagged_channels::TaggedChannels ;
10
13
//! # tokio_test::block_on(async {
11
14
//!
12
15
//! // We're going to tag channels
24
27
//! }
25
28
//!
26
29
//! // Create the manager
27
- //! let channels = SseManager::<Message, Tag>::new();
28
- //!
29
- //! // Connect and tag the channel as belonging to the user#1 who is an admin
30
- //! let stream = sse.create_stream([Tag::UserId(1), Tag::IsAdmin]).await;
31
- //!
32
- //! # let sse = axum_sse_manager::SseManager::new();
30
+ //! let mut manager = TaggedChannels::<Message, Tag>::new();
33
31
//!
34
32
//! // Message to user#1
35
- //! sse .send_by_tag(&Tag::UserId(1), Message::Ping).await;
33
+ //! manager .send_by_tag(&Tag::UserId(1), Message::Ping).await;
36
34
//!
37
35
//! // Message to all admins
38
- //! sse .send_by_tag(&Tag::UserId(1), Message::Ping).await;
36
+ //! manager .send_by_tag(&Tag::UserId(1), Message::Ping).await;
39
37
//!
40
38
//! // Message to everyone
41
- //! sse.broadcast(Message::Ping).await;
39
+ //! manager.broadcast(Message::Ping).await;
40
+ //!
41
+ //! // Connect and tag the channel as belonging to the user#1 who is an admin
42
+ //! let mut channel = manager.create_channel([Tag::UserId(1), Tag::IsAdmin]);
43
+ //!
44
+ //! // Receive events coming from the channel
45
+ //! while let Some(event) = channel.recv().await {
46
+ //! // send the event through WebSocket or SSE
47
+ //! }
42
48
//! # })
43
49
//! ```
44
50
//!
45
- //! Look at the [ full example][ example] for detail.
51
+ //! Look at the full [axum example] for detail.
46
52
//!
47
- //! [example]: https://github.com/imbolc/axum-sse-manager /blob/main/examples/users .rs
53
+ //! [axum example]: https://github.com/imbolc/tagged-channels /blob/main/examples/axum .rs
48
54
49
55
#![ warn( clippy:: all, missing_docs, nonstandard_style, future_incompatible) ]
50
56
@@ -59,7 +65,7 @@ use tokio::sync::mpsc::{self, Receiver, Sender};
59
65
type ChannelId = u64 ;
60
66
61
67
/// SSE manager
62
- pub struct TaggedChannels < M , T > ( Arc < Mutex < ManagerInner < M , T > > > ) ;
68
+ pub struct TaggedChannels < M , T > ( Arc < Mutex < ChannelsInner < M , T > > > ) ;
63
69
64
70
impl < M , T > Clone for TaggedChannels < M , T > {
65
71
fn clone ( & self ) -> Self {
@@ -68,7 +74,7 @@ impl<M, T> Clone for TaggedChannels<M, T> {
68
74
}
69
75
70
76
/// Inner part of the manager
71
- pub struct ManagerInner < M , T > {
77
+ pub struct ChannelsInner < M , T > {
72
78
last_id : u64 ,
73
79
channels : HashMap < ChannelId , Channel < M , T > > ,
74
80
tags : HashMap < T , HashSet < ChannelId > > ,
@@ -102,7 +108,7 @@ impl<M, T> TaggedChannels<M, T>
102
108
where
103
109
T : Clone + Eq + Hash + PartialEq ,
104
110
{
105
- /// Creates a new manager
111
+ /// Creates a new channels manager
106
112
pub fn new ( ) -> Self {
107
113
Default :: default ( )
108
114
}
@@ -189,7 +195,7 @@ where
189
195
190
196
impl < M , T > Default for TaggedChannels < M , T > {
191
197
fn default ( ) -> Self {
192
- let inner = ManagerInner {
198
+ let inner = ChannelsInner {
193
199
last_id : 0 ,
194
200
channels : HashMap :: new ( ) ,
195
201
tags : HashMap :: new ( ) ,
@@ -219,7 +225,7 @@ where
219
225
}
220
226
}
221
227
222
- impl < M , T > ManagerInner < M , T >
228
+ impl < M , T > ChannelsInner < M , T >
223
229
where
224
230
T : Eq + Hash + PartialEq ,
225
231
{
0 commit comments