https://github.com/freenet/replay-channel
https://github.com/freenet/replay-channel
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/freenet/replay-channel
- Owner: freenet
- License: mit
- Created: 2024-03-16T19:47:09.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-03-21T01:49:10.000Z (about 2 years ago)
- Last Synced: 2025-03-26T21:38:27.863Z (about 1 year ago)
- Language: Rust
- Size: 64.5 KB
- Stars: 5
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# 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](https://twitter.com/sanity) for the [Freenet Project](https://freenet.org/).
## 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](https://crates.io/crates/append-only-vec) 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`:
```bash
$ cargo add replay-channel
```
### Usage Example
```rust
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](LICENSE.md).