Start from Last
Subscribe to KubeMQ Events Store from the most recent stored event and continue forward using the Rust SDK.
Overview
A subscriber that just restarted usually doesn't need the entire event history — it needs to know where things stand right now without paying the cost of replaying everything that happened while it was offline. EventsStoreSubscription::StartFromLast solves that: it re-anchors a new subscription to the tail of the store, delivering exactly one historical event (the most recently stored one) before switching to live delivery. That's the sweet spot between StartFromNew (no history at all, so you might miss the current state entirely) and StartFromFirst (the full backlog, which can be slow and mostly irrelevant for a consumer that only cares about "now").
Under the hood, EventsStoreSubscription::StartFromLast is the subscription variant passed when subscribing to the events store. The broker looks up the channel's most recent stored event at subscription time, replays that single event to the new subscriber, and then streams every subsequently published event as it arrives — the same live path any other subscription uses.
Gotchas: if the channel is empty when you subscribe, there's no "last" event to deliver — you simply start receiving new events as they're published, with no error raised. StartFromLast gives you one event, not the last N — if you need a short window of recent history, replay from a sequence number instead. And because "last" is resolved at subscribe time, two subscribers starting a few events apart can each get a different one.
Prerequisites
- KubeMQ server running on
localhost:50000 - Rust SDK installed (
cargo add kubemq)
Code
use kubemq::prelude::*;
use kubemq::EventsStoreSubscription;
use std::time::Duration;
#[tokio::main]
async fn main() -> kubemq::Result<()> {
let client = KubemqClient::builder()
.host("localhost")
.port(50000)
.build()
.await?;
let sub = client.subscribe_to_events_store(
"rust-events-store.start-from-last", "",
EventsStoreSubscription::StartFromLast,
|event| Box::pin(async move {
println!("Seq {}: {}", event.sequence, String::from_utf8_lossy(&event.body));
}),
None,
).await?;
tokio::time::sleep(Duration::from_secs(5)).await;
sub.unsubscribe().await;
client.close().await?;
Ok(())
}How It Works
StartFromLastdelivers the most recent stored event, then continues with new events.- Useful when you only need to resume from where the stream currently is.
- 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?