Start from First
Subscribe and replay all events from the beginning
Overview
A new consumer joining an Events Store channel usually needs more than what happens next — it needs everything that already happened. EventStoreStartPosition.StartFromFirst solves that by replaying the channel's complete stored history before switching to live delivery, so a service can rebuild its state from scratch instead of starting with a blank slate and hoping nothing important was missed.
Under the hood, the broker walks the store from the oldest retained sequence forward, streaming each event to your subscription in order, then hands off to live delivery of new events without a gap. You don't manage offsets or checkpoints yourself — the start position is set once, at subscription time, via events_store_type=EventStoreStartPosition.StartFromFirst.
Gotchas: on a long-lived channel this can mean replaying millions of events before anything new shows up, so it's the wrong choice for a consumer that only cares about "from now on" (use StartNewOnly for that). Retention and expiration policies still apply — events already purged by TTL or max-count limits are gone and won't be replayed, so "full history" only means what the store still has.
Prerequisites
- KubeMQ server running on
localhost:50000 - Python SDK installed (
pip install kubemq)
Code
"""Example: StartFromFirst — replay all stored events from the beginning."""
from __future__ import annotations
import asyncio
from kubemq import (
AsyncCancellationToken,
AsyncPubSubClient,
EventStoreMessage,
EventsStoreSubscription,
)
from kubemq.pubsub import EventStoreStartPosition
async def main() -> None:
async with AsyncPubSubClient(
address="localhost:50000",
client_id="python-events-store-start-from-first-client",
) as client:
# Pre-populate with some messages
for i in range(5):
await client.send_event_store(
EventStoreMessage(
channel="python-events-store.start-from-first",
body=f"Message-{i + 1}".encode(),
)
)
print("Sent 5 messages")
await asyncio.sleep(1)
# Subscribe from the very first message — will replay all stored messages
token = AsyncCancellationToken()
async def subscriber() -> None:
async for event in client.subscribe_to_events_store(
subscription=EventsStoreSubscription(
channel="python-events-store.start-from-first",
on_receive_event_callback=lambda e: None,
on_error_callback=lambda e: print(f"Error: {e}"),
events_store_type=EventStoreStartPosition.StartFromFirst,
),
cancellation_token=token,
):
print(f"Received: {event.body.decode('utf-8')}")
task = asyncio.create_task(subscriber())
await asyncio.sleep(3)
token.cancel()
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
if __name__ == "__main__":
asyncio.run(main())
How It Works
EventStoreStartPosition.StartFromFirstreplays the entire event history for the channel from sequence 1, regardless of when events were stored.- The five pre-published messages are all delivered to the subscriber in sequence order immediately after the subscription is established.
- This start position is the event-sourcing rebuild pattern: a consumer can reconstruct full state by replaying all historical events.
- After delivering the history, the subscription seamlessly transitions to live delivery for any new events published to the channel.
Related
Was this page helpful?