KubeMQ
Client SDKsRustTutorials

Send Your First Message

Connect the Rust client to KubeMQ and publish and receive your first message end to end.

This is your first hands-on lesson with the Rust SDK: create a client, send an event, and receive it. Make sure you have the SDK installed (see the Rust SDK overview).

Create a Client

main.rs
use kubemq::prelude::*;

#[tokio::main]
async fn main() -> kubemq::Result<()> {
    let client = KubemqClient::builder()
        .host("localhost")
        .port(50000)
        .client_id("my-rust-service")
        .build()
        .await?;

    let info = client.ping().await?;
    println!("Connected to KubeMQ v{}", info.version);

    client.close().await?;
    Ok(())
}

Send Your First Event

main.rs
use kubemq::prelude::*;

#[tokio::main]
async fn main() -> kubemq::Result<()> {
    let client = KubemqClient::builder()
        .host("localhost")
        .port(50000)
        .build()
        .await?;

    let event = Event::builder()
        .channel("notifications")
        .body(b"hello kubemq".to_vec())
        .build();

    client.send_event(event).await?;
    println!("Event sent successfully");

    client.close().await?;
    Ok(())
}

Receive Events

main.rs
use kubemq::prelude::*;

#[tokio::main]
async fn main() -> kubemq::Result<()> {
    let client = KubemqClient::builder()
        .host("localhost")
        .port(50000)
        .build()
        .await?;

    let sub = client.subscribe_to_events(
        "notifications",
        "",
        |event| Box::pin(async move {
            println!("Received: {}", String::from_utf8_lossy(&event.body));
        }),
        None,
    ).await?;

    // Keep running until you're done
    tokio::time::sleep(std::time::Duration::from_secs(30)).await;
    sub.unsubscribe().await;
    client.close().await?;
    Ok(())
}

Configuration Options

OptionDefaultDescription
.host(h)localhostKubeMQ server hostname
.port(p)50000KubeMQ server port
.client_id(id)Auto-generated UUIDUnique client identifier
.auth_token(token)NoneAuthentication token
.tls_config(config)None (plaintext)TLS / mTLS configuration
.retry_policy(policy)3 retries, 100ms–10s backoffReconnection behavior
.connection_timeout(d)10sInitial connection timeout
.rpc_timeout(d)60sTimeout for request-response operations
.keepalive_time(d)10sHTTP/2 keepalive interval
.on_connected(cb)NoneCallback on connection establishment
.on_closed(cb)NoneCallback on connection close

Error Handling

All SDK operations return kubemq::Result<T>, which wraps KubemqError with structured error information:

main.rs
use kubemq::prelude::*;
use kubemq::{ErrorCode, KubemqError};

async fn handle_errors(client: &KubemqClient) {
    let event = Event::builder()
        .channel("test")
        .body(b"data".to_vec())
        .build();

    match client.send_event(event).await {
        Ok(()) => println!("Sent"),
        Err(ref e) if e.is_retryable() => {
            println!("Retryable error ({}): {}", e.code(), e);
        }
        Err(ref e) => {
            match e.code() {
                ErrorCode::Authentication => println!("Check credentials"),
                ErrorCode::Validation => println!("Fix request: {}", e.suggestion()),
                ErrorCode::Timeout => println!("Increase timeout"),
                _ => println!("Error: {}", e),
            }
        }
    }
}

Next Steps

Was this page helpful?

On this page