10 releases

new 0.1.7 Apr 12, 2025
0.1.6 Dec 21, 2024
0.1.5 Jun 7, 2024
0.1.3 May 5, 2024
0.1.0 Dec 25, 2023

#615 in Command line utilities

Download history 105/week @ 2024-12-21 1/week @ 2024-12-28 3/week @ 2025-01-04 1/week @ 2025-01-11 8/week @ 2025-02-08 11/week @ 2025-02-15 2/week @ 2025-02-22 1/week @ 2025-03-01

531 downloads per month
Used in liblitho

MIT license

71KB
1K SLoC

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.

API Usage

To subscribe to a topic

Client

use simple-pub-sub

#[tokio::main]
async fn main() -> Result<(), String> {
    let client_type = simple_pub_sub::client::PubSubTcpClient {
        server: "localhost".to_string(),
        port: 6480,
    };
    // initialize the client.
    let mut client =
        simple_pub_sub::client::Client::new(simple_pub_sub::client::PubSubClient::Tcp(client_type));
    // connect the client.
    client.connect().await.unwrap();
    // subscribe to the given topic.
    client.subscribe("abc".to_string()).await.unwrap();
    loop{
        let msg = client.read_message().await.unwrap();
        println!("{}: {:?}", msg.topic, msg.message)
    }

    Ok(())
}

To publish a message

use simple_pub_sub;
async fn main() -> Result<(), String> {
    let client_type = simple_pub_sub::client::PubSubTcpClient {
        server: "localhost".to_string(),
        port: 6480,
    };
    // initialize the client.
    let mut client =
        simple_pub_sub::client::Client::new(simple_pub_sub::client::PubSubClient::Tcp(client_type));
    // 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(())
}

Server

use simple_pub_sub;
async fn main(){
  let server = simple_pub_sub::server::ServerType::Tcp(
      simple_pub_sub::server::Tcp {
        host: "localhost".to_string(),
        port: 6480,
        cert: None,
        cert_password: None,
        capacity: 1024,
      });
  server.start().await;
}

Cli Usage

  • Server:

    • Using Tcp socket:

      simple-pub-sub server tcp 0.0.0.0 6480 --log-level trace
      

      To use TLS:

      # Generate the server key and certificate
      openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem \
          -days 365 -nodes -subj "/CN=localhost"
      # generate the identity file.
      openssl pkcs12 -export -out identity.pfx -inkey key.pem -in cert.pem\
        -passout pass:password
      

      Start the server using these keys:

      simple-pub-sub server tcp -c identity.pfx -p password 0.0.0.0 6480 \
        --log-level trace
      
    • Using Unix socket:

      simple-pub-sub server unix /tmp/pubsub.sock --log-level trace
      
  • Client:

    • Using Tcp socket:

      • subscribe:

        simple-pub-sub client subscribe the_topic tcp 0.0.0.0 6480 --log-level trace
        

        Using TLS:

        simple-pub-sub client subscribe the_topic tcp -c certs/cert.pem 0.0.0.0 6480\
            --log-level trace
        
      • publish:

        simple-pub-sub client publish the_topic the_message tcp 0.0.0.0 6480\
            --log-level info
        

        Using TLS:

        simple-pub-sub client publish the_topic the_message tcp -c certs/cert.pem\
         0.0.0.0 6480 --log-level trace
        
      • query:

        simple-pub-sub client query the_topic tcp 0.0.0.0 6480  --log-level trace
        

        Using TLS:

        simple-pub-sub client query the_topic tcp -c certs/cert.pem\
          0.0.0.0 6480  --log-level trace
        
    • Using Unix socket:

      • subscribe:

        simple-pub-sub client unix /tmp/pubsub.sock subscribe the_topic\
          --log-level trace
        
      • publish:

        simple-pub-sub client unix /tmp/pubsub.sock publish the_topic the_message\
          --log-level info
        
      • query:

        simple-pub-sub client unix /tmp/pubsub.sock query the_topic --log-level trace
        

Shell completion

Supported shells: bash, zsh, fish, Elvish, Powershell To add the shell completions, run

simple-pub-sub completion <shell> 

To add the shell completions to .bashrc

source <(simple-pub-sub completion bash) >> ~/.bashrc

Dependencies

~7–18MB
~267K SLoC