5 releases
0.1.2 | Apr 10, 2024 |
---|---|
0.1.1 | Jan 3, 2024 |
0.1.0 | Dec 25, 2023 |
0.1.0-rc2 | Dec 22, 2023 |
0.1.0-rc1 | Dec 20, 2023 |
#945 in Command line utilities
111 downloads per month
39KB
881 lines
Simple-pub-sub
A simple message broker implemented in rust.
The message frame looks like
header | version | pkt type | topic length | message length | padding | topic | message |
---|---|---|---|---|---|---|---|
1 byte | 2 bytes | 1 byte | 1 byte | 2 bytes | 1 byte | ..... | ..... |
So it's a 8 byte header followed by the topic and message.
Cli Usage
- Server:
simple-pub-sub server 0.0.0.0 6480 --log-level trace
-
Client:
- subscribe:
simple-pub-sub client 0.0.0.0 6480 subscribe the_topic --log-level trace
- publish:
simple-pub-sub client 0.0.0.0 6480 publish the_topic the_message --log-level info
- query:
simple-pub-sub client 0.0.0.0 6480 query the_topic --log-level trace
API Usage
To subscribe
use simple_pub_sub;
// define the on_message function (callback).
pub fn on_msg(topic: String, message: Vec<u8>) {
println!("topic: {} message: {:?}", topic, message)
}
#[tokio::main]
async fn main() -> Result<(), String> {
// initialize the client.
let mut client = simple_pub_sub::client::Client::new("localhost".to_string(), 6480);
// set the callback function.
client.on_message(on_msg);
// connect the client.
let _ = client.connect().await;
// subscribe to the given topic.
let _ = client.subscribe("abc".to_string()).await;
Ok(())
}
To push a message
use simple_pub_sub;
#[tokio::main]
async fn main() -> Result<(), String> {
// initialize the client.
let mut client = simple_pub_sub::client::Client::new("localhost".to_string(), 6480);
// connect the client.
let _ = client.connect().await;
// subscribe to the given topic.
let _ = client
.publish(
"abc".to_string(),
"test message".to_string().into_bytes().to_vec(),
)
.await;
Ok(())
}
Dependencies
~7–19MB
~242K SLoC