Start at Time Delta
Resume a KubeMQ Events Store subscription from a relative time offset in Ruby, replaying events from the last N seconds or minutes.
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.
KubeMQ::PubSub::EventStoreStartPosition::START_AT_TIME_DELTA with start_position_value 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. A start_position_value of zero replays nothing and behaves like starting from new events only. 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 - Ruby SDK installed (
gem install kubemq)
Code
require 'kubemq'
address = ENV.fetch('KUBEMQ_ADDRESS', 'localhost:50000')
channel = 'ruby-events-store.start-at-time-delta'
begin
client = KubeMQ::PubSubClient.new(address: address, client_id: 'es-delta-example')
puts "Connected to #{address}"
3.times do |i|
msg = KubeMQ::PubSub::EventStoreMessage.new(channel: channel, metadata: "event #{i}", body: "data-#{i}")
client.send_event_store(msg)
end
puts 'Pre-stored 3 events'
sleep 1
cancel = KubeMQ::CancellationToken.new
delta_seconds = 60
sub = KubeMQ::PubSub::EventsStoreSubscription.new(
channel: channel,
start_position: KubeMQ::PubSub::EventStoreStartPosition::START_AT_TIME_DELTA,
start_position_value: delta_seconds
)
client.subscribe_to_events_store(sub, cancellation_token: cancel, on_error: lambda { |e|
puts "Error: #{e.message}"
}) do |event|
puts "Received (within last #{delta_seconds}s): #{event.metadata}"
end
puts "Subscribed with START_AT_TIME_DELTA=#{delta_seconds}s"
sleep 3
rescue KubeMQ::Error => e
puts "KubeMQ error: #{e.message}"
ensure
cancel&.cancel
client&.close
puts 'Done'
endHow It Works
START_AT_TIME_DELTAreplays events stored within the last N seconds.- The
start_position_valueis the number of seconds to look back from the current time. - Useful for "catch up on recent activity" scenarios without tracking sequence numbers.
- Review timeouts, channel names, and client IDs before running against shared environments.
Related
Was this page helpful?