KubeMQ
Client SDKsC#How-to guidesEvents Store

Start from Last

Subscribe to a KubeMQ events store channel starting from the most recent stored event using the C# 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. EventStoreStartPosition.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, EventStoreStartPosition.StartFromLast is set on the StartPosition of the subscription request. 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
  • C# SDK installed (dotnet add package KubeMQ.SDK.CSharp)

Code

Program.cs
// KubeMQ .NET SDK — Events Store: Start From Last
//
// This example subscribes starting from the most recent stored event,
// then continues to receive new events as they arrive.
//
// Prerequisites:
//   - KubeMQ server running on localhost:50000
//   - dotnet run

using KubeMQ.Sdk.Client;
using KubeMQ.Sdk.EventsStore;
using System.Text;

await using var client = new KubeMQClient(new KubeMQClientOptions
{
    Address = "localhost:50000",
    ClientId = "csharp-eventsstore-start-from-last-client",
});
await client.ConnectAsync();

var subscription = new EventStoreSubscription
{
    Channel = "csharp-eventsstore.start-from-last",
    StartPosition = EventStoreStartPosition.StartFromLast,
};

Console.WriteLine("Subscribed with StartFromLast. Replaying from most recent event...");
await foreach (var evt in client.SubscribeToEventsStoreAsync(subscription))
{
    Console.WriteLine($"[Seq {evt.Sequence}] {Encoding.UTF8.GetString(evt.Body.Span)}");
}

How It Works

  • StartFromLast delivers the single most recent stored event first (if any exists), then continues streaming new events.
  • This is useful for consumers that only care about the current state — they get the latest snapshot and then stay up to date, without replaying the full history.
  • Unlike StartFromNew, which delivers no history, StartFromLast guarantees at least one event delivery if the channel is non-empty.
  • The await foreach with no cancellation token runs indefinitely; combine with CancellationTokenSource for time-bounded or programmatically stoppable subscriptions.

Was this page helpful?

On this page