Start New Only
Subscribe to a KubeMQ Events Store channel to receive only events published after subscribing, in Elixir.
Overview
Start-from-new turns a durable Events Store channel into a live-only feed — reach for it when a consumer only cares what happens from this moment forward and would rather skip a large backlog than pay to replay it. Dashboards, live notification fan-outs, and freshly-deployed services that don't need to catch up on history are the classic cases: any of the replay-from-start positions would mean churning through every historical event just to reach the live tail.
It works by passing start_at: :start_new_only to subscribe_to_events_store — the broker stamps the subscription's registration time as a watermark and delivers only events published after it, ignoring everything already stored. Gotchas: there's a race between registering and the publisher sending — a publish that lands before the broker fully registers you is silently skipped, so give the subscription a moment to settle before publishing; this position can never see anything published earlier, so use :start_from_first or a sequence-based position when you need guaranteed replay; and reconnecting doesn't resume where you left off — a fresh :start_new_only subscription starts from "now" again, with no cursor persisted across restarts.
Prerequisites
- KubeMQ server running on
localhost:50000 - Elixir SDK installed (
{:kubemq, "~> 1.0"}in mix.exs)
Code
channel = "elixir-events-store.start-new-only"
{:ok, client} = KubeMQ.Client.start_link(address: "localhost:50000", client_id: "elixir-es-new")
{:ok, _} = KubeMQ.Client.send_event_store(client,
KubeMQ.EventStore.new(channel: channel, body: "Before subscribe"))
IO.puts("Sent event before subscribing")
parent = self()
{:ok, sub} =
KubeMQ.Client.subscribe_to_events_store(client, channel,
start_at: :start_new_only,
on_event: fn event ->
IO.puts("Received: #{event.body}")
send(parent, :received)
end
)
IO.puts("Subscribed with :start_new_only")
Process.sleep(500)
{:ok, _} = KubeMQ.Client.send_event_store(client,
KubeMQ.EventStore.new(channel: channel, body: "After subscribe"))
receive do
:received -> IO.puts("Only the post-subscribe event was received!")
after
5_000 -> IO.puts("Timeout")
end
KubeMQ.Subscription.cancel(sub)
KubeMQ.Client.close(client)How It Works
:start_new_onlyskips all previously stored events- Only events published after the subscription is active are delivered
- The pre-subscribe event ("Before subscribe") is stored but not delivered
Related
Was this page helpful?