Start New Only
Subscribe to a KubeMQ Events Store channel for new events only in Ruby, skipping stored history and receiving only future events.
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 setting start_position: KubeMQ::PubSub::EventStoreStartPosition::START_NEW_ONLY on the EventsStoreSubscription passed 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 a start-from-first or start-from-last 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 - Ruby SDK installed (
gem install kubemq)
Code
require 'kubemq'
address = ENV.fetch('KUBEMQ_ADDRESS', 'localhost:50000')
channel = 'ruby-events-store.start-new-only'
begin
client = KubeMQ::PubSubClient.new(address: address, client_id: 'es-new-only-example')
puts "Connected to #{address}"
cancel = KubeMQ::CancellationToken.new
sub = KubeMQ::PubSub::EventsStoreSubscription.new(
channel: channel,
start_position: KubeMQ::PubSub::EventStoreStartPosition::START_NEW_ONLY
)
client.subscribe_to_events_store(sub, cancellation_token: cancel, on_error: lambda { |e|
puts "Error: #{e.message}"
}) do |event|
puts "Received new event: #{event.metadata}"
end
puts 'Subscribed with START_NEW_ONLY — only future events will arrive'
sleep 1
3.times do |i|
msg = KubeMQ::PubSub::EventStoreMessage.new(channel: channel, metadata: "new event #{i}", body: "data-#{i}")
client.send_event_store(msg)
puts "Sent event #{i}"
end
sleep 2
rescue KubeMQ::Error => e
puts "KubeMQ error: #{e.message}"
ensure
cancel&.cancel
client&.close
puts 'Done'
endHow It Works
START_NEW_ONLYskips all historical events and only delivers events published after the subscription starts.- This is the simplest start position — no historical replay, no sequence tracking.
- Review timeouts, channel names, and client IDs before running against shared environments.
Related
Was this page helpful?