Start at Time Delta
Replay KubeMQ Events Store messages from a relative time offset in the past using the Elixir 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.
start_at: {:start_at_time_delta, 60_000} passes the offset in milliseconds 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. The value is specified in milliseconds, not seconds, so double-check the unit when porting a delta from another SDK. 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 - Elixir SDK installed (
{:kubemq, "~> 1.0"}in mix.exs)
Code
channel = "elixir-events-store.start-at-time-delta"
{:ok, client} = KubeMQ.Client.start_link(address: "localhost:50000", client_id: "elixir-es-delta")
for i <- 1..3 do
{:ok, _} = KubeMQ.Client.send_event_store(client,
KubeMQ.EventStore.new(channel: channel, body: "Recent event #{i}"))
end
IO.puts("Subscribing with time delta of 60 seconds (60_000ms)...")
parent = self()
{:ok, sub} =
KubeMQ.Client.subscribe_to_events_store(client, channel,
start_at: {:start_at_time_delta, 60_000},
on_event: fn event ->
IO.puts("Replayed: #{event.body} (seq: #{event.sequence})")
send(parent, :replayed)
end
)
for _ <- 1..3 do
receive do
:replayed -> :ok
after
5_000 -> :timeout
end
end
IO.puts("Replayed events from the last 60 seconds")
KubeMQ.Subscription.cancel(sub)
KubeMQ.Client.close(client)How It Works
{:start_at_time_delta, 60_000}replays events from the last 60 seconds (60,000 milliseconds)- The delta is relative to the server's current time
- Useful for catching up after a brief disconnection
Related
Was this page helpful?