From f97076870376cf64e0b8f8f59c312ade6bdf0153 Mon Sep 17 00:00:00 2001 From: imbolc Date: Mon, 20 Feb 2023 21:12:05 +0600 Subject: [PATCH] Renaming --- Cargo.toml | 4 ++-- README.md | 37 ++++++++++++++++++++++--------------- examples/axum.rs | 4 ++-- src/lib.rs | 44 +++++++++++++++++++++++++------------------- 4 files changed, 51 insertions(+), 38 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a22ca12..d7e0876 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,9 @@ [package] -description = "SSE channels manager for Axum framework" +description = "Tag channels and message using tags (WebSockets, SSE users management)" edition = "2021" license = "MIT" name = "tagged-channels" -repository = "https://github.com/imbolc/axum-sse-manager" +repository = "https://github.com/imbolc/tagged-channels" version = "0.0.1" [dependencies] diff --git a/README.md b/README.md index 38b1673..59ab9e9 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,15 @@ -[![License](https://img.shields.io/crates/l/axum-sse-manager.svg)](https://choosealicense.com/licenses/mit/) -[![Crates.io](https://img.shields.io/crates/v/axum-sse-manager.svg)](https://crates.io/crates/axum-sse-manager) -[![Docs.rs](https://docs.rs/axum-sse-manager/badge.svg)](https://docs.rs/axum-sse-manager) +[![License](https://img.shields.io/crates/l/tagged-channels.svg)](https://choosealicense.com/licenses/mit/) +[![Crates.io](https://img.shields.io/crates/v/tagged-channels.svg)](https://crates.io/crates/tagged-channels) +[![Docs.rs](https://docs.rs/tagged-channels/badge.svg)](https://docs.rs/tagged-channels) -# tagged-channel +# tagged-channels -SSE channels manager for Axum framework +This library makes it easy to tag (WebSocket, SSE, ...) channels with e.g. user-id and then +send events to all the channels opened by a particular user. It's framework agnostic, but for +now has only an [axum example]. If you're using it with another framework, consider PR-ing an +adapted example. ## Usage @@ -27,25 +30,29 @@ enum Message { } // Create the manager -let channels = SseManager::::new(); - -// Connect and tag the channel as belonging to the user#1 who is an admin -let stream = sse.create_stream([Tag::UserId(1), Tag::IsAdmin]).await; - +let mut manager = TaggedChannels::::new(); // Message to user#1 -sse.send_by_tag(&Tag::UserId(1), Message::Ping).await; +manager.send_by_tag(&Tag::UserId(1), Message::Ping).await; // Message to all admins -sse.send_by_tag(&Tag::UserId(1), Message::Ping).await; +manager.send_by_tag(&Tag::UserId(1), Message::Ping).await; // Message to everyone -sse.broadcast(Message::Ping).await; +manager.broadcast(Message::Ping).await; + +// Connect and tag the channel as belonging to the user#1 who is an admin +let mut channel = manager.create_channel([Tag::UserId(1), Tag::IsAdmin]); + +// Receive events coming from the channel +while let Some(event) = channel.recv().await { + // send the event through WebSocket or SSE +} ``` -Look at the [full example][example] for detail. +Look at the full [axum example] for detail. -[example]: https://github.com/imbolc/axum-sse-manager/blob/main/examples/users.rs +[axum example]: https://github.com/imbolc/tagged-channels/blob/main/examples/axum.rs diff --git a/examples/axum.rs b/examples/axum.rs index 12e3ac0..3769d9b 100644 --- a/examples/axum.rs +++ b/examples/axum.rs @@ -120,10 +120,10 @@ async fn ws_events( Query(params): Query, State(channels): State>, ) -> impl IntoResponse { - ws.on_upgrade(move |socket| ws_socket(socket, channels, params.as_tags())) + ws.on_upgrade(move |socket| handle_socket(socket, channels, params.as_tags())) } -async fn ws_socket( +async fn handle_socket( mut socket: WebSocket, mut channels: TaggedChannels, tags: Vec, diff --git a/src/lib.rs b/src/lib.rs index 86c08dd..c344f32 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,12 +1,15 @@ -//! # tagged-channel +//! # tagged-channels //! -//! SSE channels manager for Axum framework +//! This library makes it easy to tag (WebSocket, SSE, ...) channels with e.g. user-id and then +//! send events to all the channels opened by a particular user. It's framework agnostic, but for +//! now has only an [axum example]. If you're using it with another framework, consider PR-ing an +//! adapted example. //! //! ## Usage //! //! ```rust,no_run //! # use serde::{Deserialize, Serialize}; -//! # use tagged_channels::SseManager; +//! # use tagged_channels::TaggedChannels; //! # tokio_test::block_on(async { //! //! // We're going to tag channels @@ -24,27 +27,30 @@ //! } //! //! // Create the manager -//! let channels = SseManager::::new(); -//! -//! // Connect and tag the channel as belonging to the user#1 who is an admin -//! let stream = sse.create_stream([Tag::UserId(1), Tag::IsAdmin]).await; -//! -//! # let sse = axum_sse_manager::SseManager::new(); +//! let mut manager = TaggedChannels::::new(); //! //! // Message to user#1 -//! sse.send_by_tag(&Tag::UserId(1), Message::Ping).await; +//! manager.send_by_tag(&Tag::UserId(1), Message::Ping).await; //! //! // Message to all admins -//! sse.send_by_tag(&Tag::UserId(1), Message::Ping).await; +//! manager.send_by_tag(&Tag::UserId(1), Message::Ping).await; //! //! // Message to everyone -//! sse.broadcast(Message::Ping).await; +//! manager.broadcast(Message::Ping).await; +//! +//! // Connect and tag the channel as belonging to the user#1 who is an admin +//! let mut channel = manager.create_channel([Tag::UserId(1), Tag::IsAdmin]); +//! +//! // Receive events coming from the channel +//! while let Some(event) = channel.recv().await { +//! // send the event through WebSocket or SSE +//! } //! # }) //! ``` //! -//! Look at the [full example][example] for detail. +//! Look at the full [axum example] for detail. //! -//! [example]: https://github.com/imbolc/axum-sse-manager/blob/main/examples/users.rs +//! [axum example]: https://github.com/imbolc/tagged-channels/blob/main/examples/axum.rs #![warn(clippy::all, missing_docs, nonstandard_style, future_incompatible)] @@ -59,7 +65,7 @@ use tokio::sync::mpsc::{self, Receiver, Sender}; type ChannelId = u64; /// SSE manager -pub struct TaggedChannels(Arc>>); +pub struct TaggedChannels(Arc>>); impl Clone for TaggedChannels { fn clone(&self) -> Self { @@ -68,7 +74,7 @@ impl Clone for TaggedChannels { } /// Inner part of the manager -pub struct ManagerInner { +pub struct ChannelsInner { last_id: u64, channels: HashMap>, tags: HashMap>, @@ -102,7 +108,7 @@ impl TaggedChannels where T: Clone + Eq + Hash + PartialEq, { - /// Creates a new manager + /// Creates a new channels manager pub fn new() -> Self { Default::default() } @@ -189,7 +195,7 @@ where impl Default for TaggedChannels { fn default() -> Self { - let inner = ManagerInner { + let inner = ChannelsInner { last_id: 0, channels: HashMap::new(), tags: HashMap::new(), @@ -219,7 +225,7 @@ where } } -impl ManagerInner +impl ChannelsInner where T: Eq + Hash + PartialEq, {