Connect
Create a KubeMQ client connection with builder options and verify it with a ping using the Rust SDK.
Overview
Every KubeMQ application starts the same way: open a connection to the broker and prove it actually works before building anything on top of it. This tutorial is that first lesson — create a client with the builder, give it a stable identity, and confirm connectivity with a health check, so the pattern is muscle memory before you move on to real messaging.
KubemqClient::builder() configures the host, port, and client_id — the ID tags this connection in broker logs, subscriptions, and management views, so pick something stable rather than a random string. Calling .check_connection(true) before .build().await? makes construction fail fast if the broker isn't reachable, instead of deferring discovery to the first real operation. client.ping().await? verifies the round trip cheaply, returning live server info instead of just "no error." client.close().await? releases the underlying connection.
Gotchas: without check_connection(true), a successful build() doesn't always mean the broker is reachable, so ping() is the only reliable proof; reusing the same client ID across running instances causes routing confusion on the broker; and forgetting client.close() in quick scripts is a common source of leaked connections under load.
Prerequisites
- KubeMQ server running on
localhost:50000 - Rust SDK installed (
cargo add kubemq)
Code
use kubemq::prelude::*;
#[tokio::main]
async fn main() -> kubemq::Result<()> {
// Minimal connection
let client = KubemqClient::builder()
.host("localhost")
.port(50000)
.build()
.await?;
let info = client.ping().await?;
println!("Minimal client connected. Server: {}", info.version);
client.close().await?;
// Connection with client ID and check_connection
let client = KubemqClient::builder()
.host("localhost")
.port(50000)
.client_id("rust-example-client")
.check_connection(true)
.build()
.await?;
let info = client.ping().await?;
println!("Configured client connected. Server: {}", info.version);
client.close().await?;
Ok(())
}How It Works
- The builder pattern configures connection parameters before calling
.build().await?. check_connection(true)verifies the broker is reachable during client construction.- Review timeouts, channel names, and client IDs before running against shared environments.
- Run the program while the server from the prerequisites is available.
Related
Was this page helpful?