KubeMQ
Client SDKsNode.jsHow-to guidesEvents Store

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 onEvent callback 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 startFrom: 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
  • Node.js SDK installed (npm install kubemq-js)

Code

start-from-first.ts
/**
 * Example: Replay All Events from the Beginning
 *
 * Demonstrates subscribing to an event store starting from the very first
 * stored event. Useful for rebuilding state from scratch (event sourcing).
 *
 * Prerequisites:
 *   - KubeMQ server running on localhost:50000
 *
 * Run: npx tsx examples/events-store/start-from-first.ts
 */
import { KubeMQClient, createEventStoreMessage, EventStoreStartPosition } from 'kubemq-js';

async function main(): Promise<void> {
  const client = await KubeMQClient.create({
    address: 'localhost:50000',
    clientId: 'js-events-store-start-from-first-client',
  });

  try {
    // Publish some events first.
    const events = ['User created', 'Profile updated', 'Email verified'];
    for (const action of events) {
      await client.sendEventStore(
        createEventStoreMessage({
          channel: 'js-events-store.start-from-first',
          body: action,
        }),
      );
    }

    // Replay all events from the very first one.
    const subscription = client.subscribeToEventsStore({
      channel: 'js-events-store.start-from-first',
      startFrom: EventStoreStartPosition.StartFromFirst,
      onEvent: (event) => {
        console.log(`[seq=${event.sequence}] ${new TextDecoder().decode(event.body)}`);
      },
      onError: (err) => {
        console.error('Subscription error:', err.message);
      },
    });

    await new Promise((resolve) => setTimeout(resolve, 2000));
    subscription.cancel();
  } finally {
    await client.close();
  }
}

main().catch(console.error);

How It Works

  • StartFromFirst replays every event ever stored on the channel, starting from sequence 1, regardless of when they were published.
  • Three events are published before subscribing — StartFromFirst ensures the subscriber receives all of them as a replay batch before continuing with new events.
  • This is the canonical pattern for event-sourcing use cases where a consumer needs to rebuild state from scratch by processing the full history.
  • After the replay batch completes, the subscription automatically transitions to live delivery of any new events published to the channel.

Was this page helpful?

On this page