Start at Time Delta
Subscribe to a KubeMQ Events Store channel from a relative time offset in Node.js, replaying events from the last N seconds or minutes.
Overview
A time-delta subscription starts replay from a relative offset — "the last 30 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 startValue passes the offset in seconds 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. If no events fall inside the window, the subscription simply delivers only future events from that point on. 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 - Node.js SDK installed (
npm install kubemq-js)
Code
import { KubeMQClient, EventStoreStartPosition } from 'kubemq-js';
async function main() {
const client = await KubeMQClient.create({
address: 'localhost:50000',
clientId: 'js-events-store-start-at-time-delta-client',
});
try {
const sub = client.subscribeToEventsStore({
channel: 'js-events-store.start-at-time-delta',
startFrom: EventStoreStartPosition.StartAtTimeDelta,
startValue: 30,
onEvent: (event) => {
console.log(`[seq=${event.sequence}]`, new TextDecoder().decode(event.body));
},
onError: (err) => {
console.error('Error:', err.message);
},
});
console.log('Subscribed with StartAtTimeDelta=30s — replays events from the last 30 seconds');
await new Promise((r) => setTimeout(r, 2000));
sub.cancel();
} finally {
await client.close();
}
}
main().catch(console.error);
How It Works
StartAtTimeDeltawithstartValue: 30asks the server to replay all events published within the last 30 seconds from the moment of subscription.- If no events exist within that window, the subscription immediately switches to
StartFromNewsemantics and delivers only future events. - The example subscribes without publishing first — if the channel has recent events they are replayed; otherwise no events arrive and the 2-second wait exits cleanly.
- Compare with
StartAtSequencewhen you need a precise checkpoint, orStartFromFirstwhen you want the full channel history regardless of time.
Related
Was this page helpful?