#tagged #channel #message #manager #axum #user-management #deserialize

tagged-channels

Tag channels and message using tags (WebSockets, SSE users management)

2 unstable releases

Uses new Rust 2024

0.1.0 May 20, 2025
0.0.1 Feb 20, 2023

#422 in WebSocket

Download history 10/week @ 2026-01-04 10/week @ 2026-01-11 1/week @ 2026-01-18 2/week @ 2026-01-25 20/week @ 2026-02-01 28/week @ 2026-02-08 21/week @ 2026-02-15 10/week @ 2026-03-08

132 downloads per month

MIT license

15KB
169 lines

License Crates.io Docs.rs

tagged-channels

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

# use serde::{Deserialize, Serialize};
# use tagged_channels::TaggedChannels;
# tokio_test::block_on(async {
// We're going to tag channels
#[derive(Clone, Eq, Hash, PartialEq)]
enum Tag {
    UserId(i32),
    IsAdmin,
}

// Events we're going to send
#[derive(Deserialize, Serialize)]
#[serde(tag = "_type")]
enum Message {
    Ping,
}

// Create the manager
let mut manager = TaggedChannels::<Message, Tag>::new();

// Message to user#1
manager.send_by_tag(&Tag::UserId(1), Message::Ping).await;

// Message to all admins
manager.send_by_tag(&Tag::IsAdmin, Message::Ping).await;

// Message to everyone
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 axum example for detail.

Contributing

Please run.pre-commit.sh before sending a PR, it will check everything.

License

This project is licensed under the MIT license.

Dependencies

~2–3MB
~45K SLoC