Start at Time Delta
Subscribe to KubeMQ Events Store starting from a relative time offset using the Go SDK to replay recent events.
Overview
A time-delta subscription starts replay from a relative offset — "the last 30 minutes" — 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.
kubemq.StartFromTimeDelta(30*time.Minute) passes the duration 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. A delta of zero behaves like StartFromNewEvents (no replay). 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 - Go SDK installed (
go get github.com/kubemq-io/kubemq-go/v2)
Code
// Example: events-store/start-at-time-delta
//
// Demonstrates subscribing to event store with StartFromTimeDelta.
// Events are replayed from (now - delta). For example, 30 minutes ago.
//
// Channel: go-events-store.start-at-time-delta
// Client ID: go-events-store-start-at-time-delta-client
//
// Run with a KubeMQ server on localhost:50000
// (see https://docs.kubemq.io/deploy).
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/kubemq-io/kubemq-go/v2"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
client, err := kubemq.NewClient(ctx,
kubemq.WithAddress("localhost", 50000),
kubemq.WithClientId("go-events-store-start-at-time-delta-client"),
)
if err != nil {
log.Fatal(err)
}
defer client.Close()
channel := "go-events-store.start-at-time-delta"
received := make(chan struct{}, 5)
// Subscribe starting from 30 minutes ago.
sub, err := client.SubscribeToEventsStore(ctx, channel, "",
kubemq.StartFromTimeDelta(30*time.Minute),
kubemq.WithOnEventStoreReceive(func(e *kubemq.EventStoreReceive) {
fmt.Printf("[StartFromTimeDelta] seq=%d body=%s\n", e.Sequence, string(e.Body))
select {
case received <- struct{}{}:
default:
}
}),
kubemq.WithOnError(func(err error) {
log.Println("Subscription error:", err)
}),
)
if err != nil {
log.Fatal(err)
}
defer sub.Unsubscribe()
// Send an event that falls within the time delta window.
_, err = client.SendEventStore(ctx, kubemq.NewEventStore().
SetChannel(channel).
SetBody([]byte("recent event")))
if err != nil {
log.Fatal(err)
}
select {
case <-received:
fmt.Println("Start at time delta demo complete")
case <-ctx.Done():
log.Fatal("Timed out")
}
}
How It Works
kubemq.StartFromTimeDelta(30 * time.Minute)instructs the broker to replay events stored fromnow - 30mat subscription time, then continue with live events.- Unlike
StartFromTime, the delta is evaluated server-side at subscription creation, so there is no need to compute a concretetime.Timein the client. - The subscription automatically transitions from replay to live delivery once all matching historical events have been sent.
- Increase the delta (e.g.,
24 * time.Hour) for a longer backfill; set it to zero to matchStartFromNewEventsbehaviour.
Related
Was this page helpful?