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
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
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
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
| Option | Default | Description |
|---|---|---|
.host(h) | localhost | KubeMQ server hostname |
.port(p) | 50000 | KubeMQ server port |
.client_id(id) | Auto-generated UUID | Unique client identifier |
.auth_token(token) | None | Authentication token |
.tls_config(config) | None (plaintext) | TLS / mTLS configuration |
.retry_policy(policy) | 3 retries, 100ms–10s backoff | Reconnection behavior |
.connection_timeout(d) | 10s | Initial connection timeout |
.rpc_timeout(d) | 60s | Timeout for request-response operations |
.keepalive_time(d) | 10s | HTTP/2 keepalive interval |
.on_connected(cb) | None | Callback on connection establishment |
.on_closed(cb) | None | Callback on connection close |
Error Handling
All SDK operations return kubemq::Result<T>, which wraps KubemqError with structured error information:
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
- Rust SDK Reference — full API documentation
- Rust SDK Examples — complete examples for all patterns
- GitHub Repository — source code and issues
Was this page helpful?