KubeMQ
Client SDKsC#How-to guidesEvents Store

Start at Time Delta

Subscribe to a KubeMQ events store channel from a relative time offset using the C# SDK.

Overview

A time-delta subscription starts replay from a relative offset — "the last 60 seconds" — instead of a fixed timestamp or sequence number. It's the right tool when a consumer knows how long it was offline but not the exact moment it disconnected: a worker restarting after a deploy, a dashboard reconnecting after a blip, or a batch job that only cares about "recent" history. Computing an absolute cutoff yourself is bookkeeping the broker can do for you.

EventStoreStartPosition.StartAtTimeDelta with StartTimeDeltaSeconds passes the offset to the broker, which resolves it to now - delta at subscription time, replays every stored event from that point forward, then hands off to live delivery — the same replay-to-live transition as an absolute-time or sequence-based start.

Gotchas: the delta is evaluated once, server-side, at subscription creation — it does not "slide" as time passes. Larger deltas mean more history to replay before live events start flowing, adding latency to the initial connection. And since the window is wall-clock based, clock skew between producers and the broker can shift which events land inside or outside the boundary.

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 At Time Delta
//
// This example subscribes starting from events stored in the last 60 seconds,
// using a relative time delta (seconds ago) as the start position.
//
// 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-at-time-delta-client",
});
await client.ConnectAsync();

var subscription = new EventStoreSubscription
{
    Channel = "csharp-eventsstore.start-at-time-delta",
    StartPosition = EventStoreStartPosition.StartAtTimeDelta,
    StartTimeDeltaSeconds = 60,
};

Console.WriteLine("Subscribed with StartAtTimeDelta (60 seconds ago). Replaying stored events...");

var cts = new CancellationTokenSource();
Console.CancelKeyPress += (_, e) =>
{
    e.Cancel = true;
    cts.Cancel();
};

await foreach (var evt in client.SubscribeToEventsStoreAsync(subscription, cts.Token))
{
    Console.WriteLine($"[Seq {evt.Sequence}] {Encoding.UTF8.GetString(evt.Body.Span)}");
}

Console.WriteLine("Done.");

How It Works

  • StartAtTimeDelta with StartTimeDeltaSeconds = 60 is evaluated at subscription time: the broker replays events stored in the last 60 seconds, then streams live events going forward.
  • This is relative to wall clock — ideal for reconnection recovery when you know roughly how long the consumer was offline.
  • The Console.CancelKeyPress handler cancels the await foreach gracefully on Ctrl+C, avoiding an unhandled exception on shutdown.
  • Adjust StartTimeDeltaSeconds to cover your expected downtime window; larger values replay more history but may add latency before live events begin.

Was this page helpful?

On this page