Skip to content

Commit

Permalink
Renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
imbolc committed Feb 20, 2023
1 parent dbfe6e4 commit f970768
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 38 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
37 changes: 22 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

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

# 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

Expand All @@ -27,25 +30,29 @@ enum Message {
}
// Create the manager
let channels = SseManager::<Message, Tag>::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::<Message, Tag>::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

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

Expand Down
4 changes: 2 additions & 2 deletions examples/axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ async fn ws_events(
Query(params): Query<ConnectionParams>,
State(channels): State<TaggedChannels<EventMessage, ChannelTag>>,
) -> 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<EventMessage, ChannelTag>,
tags: Vec<ChannelTag>,
Expand Down
44 changes: 25 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -24,27 +27,30 @@
//! }
//!
//! // Create the manager
//! let channels = SseManager::<Message, Tag>::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::<Message, Tag>::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)]

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

/// SSE manager
pub struct TaggedChannels<M, T>(Arc<Mutex<ManagerInner<M, T>>>);
pub struct TaggedChannels<M, T>(Arc<Mutex<ChannelsInner<M, T>>>);

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

/// Inner part of the manager
pub struct ManagerInner<M, T> {
pub struct ChannelsInner<M, T> {
last_id: u64,
channels: HashMap<ChannelId, Channel<M, T>>,
tags: HashMap<T, HashSet<ChannelId>>,
Expand Down Expand Up @@ -102,7 +108,7 @@ impl<M, T> TaggedChannels<M, T>
where
T: Clone + Eq + Hash + PartialEq,
{
/// Creates a new manager
/// Creates a new channels manager
pub fn new() -> Self {
Default::default()
}
Expand Down Expand Up @@ -189,7 +195,7 @@ where

impl<M, T> Default for TaggedChannels<M, T> {
fn default() -> Self {
let inner = ManagerInner {
let inner = ChannelsInner {
last_id: 0,
channels: HashMap::new(),
tags: HashMap::new(),
Expand Down Expand Up @@ -219,7 +225,7 @@ where
}
}

impl<M, T> ManagerInner<M, T>
impl<M, T> ChannelsInner<M, T>
where
T: Eq + Hash + PartialEq,
{
Expand Down

0 comments on commit f970768

Please sign in to comment.