Skip to main content

Module hub

Module hub 

Source
Expand description

Publish/subscribe rooms

A Hub fans messages out to everyone subscribed to a named room: chat rooms, live dashboards, per-document editing sessions. Subscribers that fall behind do not slow the others down: they are told how many messages they missed and carry on with the newest ones.

use ullun::hub::{Delivery, Hub};

let hub: Hub<String> = Hub::new(64);
let mut alice = hub.subscribe("lobby");
let mut bob = hub.subscribe("lobby");
assert_eq!(hub.publish("lobby", "hello".to_string()), 2);

assert_eq!(alice.recv().await, Some(Delivery::Message("hello".to_string())));
assert_eq!(bob.recv().await, Some(Delivery::Message("hello".to_string())));
assert_eq!(hub.members("lobby"), 2);
drop(bob);
assert_eq!(hub.members("lobby"), 1);

Rooms exist while they have subscribers and disappear with the last one, so a hub cannot be filled with empty rooms. Messages are not stored: keep history yourself if late joiners need it.

Structs§

Hub
Named rooms with fan-out; cheap to clone, all clones share the rooms
Subscription
A member of a room; leaves it when dropped

Enums§

Delivery
What a subscription yields