# Send Your First Message (/sdks/rust/tutorials/first-message)



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](/sdks/rust)).

## Create a Client [#create-a-client]

```rust title="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 [#send-your-first-event]

```rust title="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 [#receive-events]

```rust title="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 [#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 [#error-handling]

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

```rust title="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 [#next-steps]

* [Rust SDK Reference](/sdks/rust/reference) — full API documentation
* [Rust SDK Examples](/sdks/rust/how-to) — complete examples for all patterns
* [GitHub Repository](https://github.com/kubemq-io/kubemq-rust) — source code and issues
