Start New Only
Receive only new events after subscription is established
Overview
Start-from-new turns a durable Events Store channel into a live-only feed — reach for it when a consumer only cares what happens from this moment forward and would rather skip a large backlog than pay to replay it. Dashboards, live notification fan-outs, and freshly-deployed services that don't need to catch up on history are the classic cases: any of the replay-from-start options would mean churning through every historical event just to reach the live tail.
It works by passing SubscriptionOption::StartFromNewEvents() to SubscribeToEventsStore — the broker stamps the subscription's registration time as a watermark and delivers only events published after it, ignoring everything already stored. Gotchas: there's a race between registering and the publisher sending — a publish that lands before the broker fully registers you is silently skipped, so give the subscription a moment to settle before publishing; this position can never see anything published earlier, so use a start-from-first or start-from-sequence option when you need guaranteed replay; and reconnecting doesn't resume where you left off — a fresh StartFromNewEvents() subscription starts from "now" again, with no cursor persisted across restarts.
Prerequisites
- KubeMQ server running on
localhost:50000 - C++ SDK installed (vcpkg or CMake FetchContent)
- C++17 compiler (GCC 9+, Clang 9+, MSVC 2019+)
Code
// Example: events_store/start_new_only
//
// Demonstrates subscribing to event store with StartFromNewEvents.
// Only events published after the subscription is established are delivered.
// Previously stored events are not replayed.
//
// Channel: cpp-events-store.start-new-only
// Client ID: cpp-events-store-start-new-only-client
//
// Run with a KubeMQ server on localhost:50000
// (see https://docs.kubemq.io/deploy).
#include <kubemq/kubemq.h>
#include <atomic>
#include <chrono>
#include <iostream>
#include <thread>
int main() {
std::cout << "[1] Connecting to localhost:50000" << std::endl;
kubemq::ClientOptions options;
options.set_address("localhost", 50000);
options.set_client_id("cpp-events-store-start-new-only-client");
auto client_result = kubemq::Client::Create(options);
if (!client_result.ok()) {
std::cerr << "[ERROR] Failed to create client: " << client_result.status().message()
<< std::endl;
return 1;
}
auto& client = *client_result;
std::string channel = "cpp-events-store.start-new-only";
std::atomic<int> received_count{0};
// Subscribe with StartFromNewEvents -- only new events are delivered.
std::cout << "[2] Subscribing with StartFromNewEvents" << std::endl;
auto sub_result = client->SubscribeToEventsStore(
channel, "", kubemq::SubscriptionOption::StartFromNewEvents(),
[&received_count](const kubemq::EventStoreReceive& e) {
std::cout << "[4] [StartFromNew] seq=" << e.sequence << " body=" << e.body << std::endl;
received_count.fetch_add(1);
},
[](const kubemq::Status& err) {
std::cerr << "[ERROR] Subscription error: " << err.message() << std::endl;
});
if (!sub_result.ok()) {
std::cerr << "[ERROR] SubscribeToEventsStore: " << sub_result.status().message()
<< std::endl;
return 1;
}
auto& sub = *sub_result;
// Allow time for subscription to register on server.
std::this_thread::sleep_for(std::chrono::seconds(1));
// Send an event after subscribing.
std::cout << "[3] Sending event after subscription" << std::endl;
auto ev_result = kubemq::EventStore::Builder()
.SetChannel(channel)
.SetBody("new event only")
.SetMetadata("new-only")
.Build();
if (!ev_result.ok()) {
std::cerr << "[ERROR] Build: " << ev_result.status().message() << std::endl;
return 1;
}
auto send_result = client->SendEventStore(*ev_result);
if (!send_result.ok()) {
std::cerr << "[ERROR] SendEventStore: " << send_result.status().message() << std::endl;
return 1;
}
// Wait for the event to be received.
std::this_thread::sleep_for(std::chrono::seconds(2));
std::cout << "[5] Received " << received_count.load() << " event(s)" << std::endl;
std::cout << "[6] Start new only demo complete" << std::endl;
// Cancel the subscription explicitly.
// Note: The Subscription destructor also calls Cancel(), but explicit
// cleanup is shown here for clarity and to match Go's defer pattern.
sub->Cancel();
auto close_status = client->Close();
if (!close_status.ok()) {
std::cerr << "[ERROR] Close failed: " << close_status.message() << std::endl;
return 1;
}
std::cout << "[7] Client closed" << std::endl;
return 0;
}How It Works
- Subscribes with
StartFromNewEvents()-- previously stored events are not replayed. - Only events published after the subscription is established are delivered.
- Sends an event after subscribing and verifies it is received.
- Useful when you only care about future events, not historical data.
Related
Was this page helpful?