Skip to content

Commit f970768

Browse files
committed
Renaming
1 parent dbfe6e4 commit f970768

File tree

4 files changed

+51
-38
lines changed

4 files changed

+51
-38
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[package]
2-
description = "SSE channels manager for Axum framework"
2+
description = "Tag channels and message using tags (WebSockets, SSE users management)"
33
edition = "2021"
44
license = "MIT"
55
name = "tagged-channels"
6-
repository = "https://github.com/imbolc/axum-sse-manager"
6+
repository = "https://github.com/imbolc/tagged-channels"
77
version = "0.0.1"
88

99
[dependencies]

README.md

+22-15
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
[![License](https://img.shields.io/crates/l/axum-sse-manager.svg)](https://choosealicense.com/licenses/mit/)
2-
[![Crates.io](https://img.shields.io/crates/v/axum-sse-manager.svg)](https://crates.io/crates/axum-sse-manager)
3-
[![Docs.rs](https://docs.rs/axum-sse-manager/badge.svg)](https://docs.rs/axum-sse-manager)
1+
[![License](https://img.shields.io/crates/l/tagged-channels.svg)](https://choosealicense.com/licenses/mit/)
2+
[![Crates.io](https://img.shields.io/crates/v/tagged-channels.svg)](https://crates.io/crates/tagged-channels)
3+
[![Docs.rs](https://docs.rs/tagged-channels/badge.svg)](https://docs.rs/tagged-channels)
44

55
<!-- cargo-sync-readme start -->
66

7-
# tagged-channel
7+
# tagged-channels
88

9-
SSE channels manager for Axum framework
9+
This library makes it easy to tag (WebSocket, SSE, ...) channels with e.g. user-id and then
10+
send events to all the channels opened by a particular user. It's framework agnostic, but for
11+
now has only an [axum example]. If you're using it with another framework, consider PR-ing an
12+
adapted example.
1013

1114
## Usage
1215

@@ -27,25 +30,29 @@ enum Message {
2730
}
2831
2932
// Create the manager
30-
let channels = SseManager::<Message, Tag>::new();
31-
32-
// Connect and tag the channel as belonging to the user#1 who is an admin
33-
let stream = sse.create_stream([Tag::UserId(1), Tag::IsAdmin]).await;
34-
33+
let mut manager = TaggedChannels::<Message, Tag>::new();
3534
3635
// Message to user#1
37-
sse.send_by_tag(&Tag::UserId(1), Message::Ping).await;
36+
manager.send_by_tag(&Tag::UserId(1), Message::Ping).await;
3837
3938
// Message to all admins
40-
sse.send_by_tag(&Tag::UserId(1), Message::Ping).await;
39+
manager.send_by_tag(&Tag::UserId(1), Message::Ping).await;
4140
4241
// Message to everyone
43-
sse.broadcast(Message::Ping).await;
42+
manager.broadcast(Message::Ping).await;
43+
44+
// Connect and tag the channel as belonging to the user#1 who is an admin
45+
let mut channel = manager.create_channel([Tag::UserId(1), Tag::IsAdmin]);
46+
47+
// Receive events coming from the channel
48+
while let Some(event) = channel.recv().await {
49+
// send the event through WebSocket or SSE
50+
}
4451
```
4552

46-
Look at the [full example][example] for detail.
53+
Look at the full [axum example] for detail.
4754

48-
[example]: https://github.com/imbolc/axum-sse-manager/blob/main/examples/users.rs
55+
[axum example]: https://github.com/imbolc/tagged-channels/blob/main/examples/axum.rs
4956

5057
<!-- cargo-sync-readme end -->
5158

examples/axum.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ async fn ws_events(
120120
Query(params): Query<ConnectionParams>,
121121
State(channels): State<TaggedChannels<EventMessage, ChannelTag>>,
122122
) -> impl IntoResponse {
123-
ws.on_upgrade(move |socket| ws_socket(socket, channels, params.as_tags()))
123+
ws.on_upgrade(move |socket| handle_socket(socket, channels, params.as_tags()))
124124
}
125125

126-
async fn ws_socket(
126+
async fn handle_socket(
127127
mut socket: WebSocket,
128128
mut channels: TaggedChannels<EventMessage, ChannelTag>,
129129
tags: Vec<ChannelTag>,

src/lib.rs

+25-19
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
//! # tagged-channel
1+
//! # tagged-channels
22
//!
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.
47
//!
58
//! ## Usage
69
//!
710
//! ```rust,no_run
811
//! # use serde::{Deserialize, Serialize};
9-
//! # use tagged_channels::SseManager;
12+
//! # use tagged_channels::TaggedChannels;
1013
//! # tokio_test::block_on(async {
1114
//!
1215
//! // We're going to tag channels
@@ -24,27 +27,30 @@
2427
//! }
2528
//!
2629
//! // 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();
3331
//!
3432
//! // 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;
3634
//!
3735
//! // 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;
3937
//!
4038
//! // 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+
//! }
4248
//! # })
4349
//! ```
4450
//!
45-
//! Look at the [full example][example] for detail.
51+
//! Look at the full [axum example] for detail.
4652
//!
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
4854
4955
#![warn(clippy::all, missing_docs, nonstandard_style, future_incompatible)]
5056

@@ -59,7 +65,7 @@ use tokio::sync::mpsc::{self, Receiver, Sender};
5965
type ChannelId = u64;
6066

6167
/// SSE manager
62-
pub struct TaggedChannels<M, T>(Arc<Mutex<ManagerInner<M, T>>>);
68+
pub struct TaggedChannels<M, T>(Arc<Mutex<ChannelsInner<M, T>>>);
6369

6470
impl<M, T> Clone for TaggedChannels<M, T> {
6571
fn clone(&self) -> Self {
@@ -68,7 +74,7 @@ impl<M, T> Clone for TaggedChannels<M, T> {
6874
}
6975

7076
/// Inner part of the manager
71-
pub struct ManagerInner<M, T> {
77+
pub struct ChannelsInner<M, T> {
7278
last_id: u64,
7379
channels: HashMap<ChannelId, Channel<M, T>>,
7480
tags: HashMap<T, HashSet<ChannelId>>,
@@ -102,7 +108,7 @@ impl<M, T> TaggedChannels<M, T>
102108
where
103109
T: Clone + Eq + Hash + PartialEq,
104110
{
105-
/// Creates a new manager
111+
/// Creates a new channels manager
106112
pub fn new() -> Self {
107113
Default::default()
108114
}
@@ -189,7 +195,7 @@ where
189195

190196
impl<M, T> Default for TaggedChannels<M, T> {
191197
fn default() -> Self {
192-
let inner = ManagerInner {
198+
let inner = ChannelsInner {
193199
last_id: 0,
194200
channels: HashMap::new(),
195201
tags: HashMap::new(),
@@ -219,7 +225,7 @@ where
219225
}
220226
}
221227

222-
impl<M, T> ManagerInner<M, T>
228+
impl<M, T> ChannelsInner<M, T>
223229
where
224230
T: Eq + Hash + PartialEq,
225231
{

0 commit comments

Comments
 (0)