Stream Send
High-throughput queue publishing via upstream stream.
Overview
Sending one queue message per call works fine for occasional traffic, but each call carries its own round trip. At high volume — event ingestion, sensor telemetry, log shipping — that per-call overhead caps your throughput well below what the connection can support.
client.send_queue_message(msg) sends each message as an individual RPC call. For higher throughput, the SDK also exposes client.queue_upstream(), which opens a persistent gRPC stream that batches multiple messages over one connection instead of paying the per-message RPC cost — the right choice once you're publishing enough volume that connection overhead, not bandwidth, is the bottleneck.
Gotchas: the simple send_queue_message loop shown here awaits each send before starting the next, so throughput is bounded by round-trip latency — don't reach for it in a hot ingestion path; switch to queue_upstream() instead. Pick unique, deliberate channel names and client IDs before running against a shared server — the example's channel is easy to collide with across runs. Always client.close().await when done so the connection is released cleanly rather than left to time out.
Prerequisites
- KubeMQ server running on
localhost:50000 - Rust SDK installed (
cargo add kubemq)
Code
use kubemq::prelude::*;
use kubemq::QueueMessageBuilder;
#[tokio::main]
async fn main() -> kubemq::Result<()> {
let client = KubemqClient::builder()
.host("localhost")
.port(50000)
.build()
.await?;
let channel = "rust-queues.stream-send";
for i in 0..20 {
let msg = QueueMessageBuilder::new()
.channel(channel)
.body(format!("stream-msg-{}", i).into_bytes())
.build();
client.send_queue_message(msg).await?;
}
println!("Sent 20 messages to queue: {}", channel);
client.close().await?;
Ok(())
}How It Works
- For high-throughput scenarios, consider using the queue stream upstream API (
client.queue_upstream()). - The simple API (
send_queue_message) sends each message individually over a single RPC call. - The upstream stream API batches multiple messages over a single persistent gRPC connection for lower per-message overhead.
- 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?