#messages #replay #channel #receiver #broadcast-channel #receiving #past

replay-channel

A broadcast channel where new receivers replay all past messages before receiving new ones

12 releases

0.1.12 Mar 19, 2024
0.1.11 Mar 17, 2024

#450 in Asynchronous

Download history 520/week @ 2024-03-14 65/week @ 2024-03-21 7/week @ 2024-03-28 1/week @ 2024-04-04

112 downloads per month

Apache-2.0

14KB
210 lines

ReplayChannel

ReplayChannel is a Rust library that lets you create a channel where messages are broadcast to all receivers. Importantly, if a new receiver is added later, they'll get all previously sent messages until they are caught up with the sender.

Developed by Ian Clarke for the Freenet Project.

Features

  • Message Replay: New receivers are sent all previously sent messages until they are caught up with the sender.
  • Multi-Receiver: Supports multiple receivers, each with its own view of the message history and real-time stream.
  • Asynchronous: Designed to be used with Tokio, async-std, or any other async runtime.
  • Efficient: Uses an AppendOnlyVec to store sent messages, avoiding locks.

Memory Usage

ReplayChannel stores all sent messages, so the memory usage is proportional to the number of messages sent. Because of this the number of messages sent should be bounded.

Getting Started

To use ReplayChannel, add it to your project's Cargo.toml:

$ cargo add replay-channel

Usage Example

let replay_channel = ReplayChannel::new();
let sender = replay_channel.sender();
sender.send("message 1");
sender.send("message 2");

let mut receiver = replay_channel.receiver();
assert_eq!(receiver.receive().await, "message 1");
assert_eq!(receiver.receive().await, "message 2");

let mut new_receiver = replay_channel.receiver();
assert_eq!(new_receiver.receive().await, "message 1");
assert_eq!(new_receiver.receive().await, "message 2");

sender.send("message 3");
assert_eq!(new_receiver.receive().await, "message 3");

License

Available under the MIT license.

Dependencies

~2.4–9.5MB
~57K SLoC