KubeMQ
Client SDKsNode.jsHow-to guidesEvents Store

Start New Only

Subscribe to a KubeMQ Events Store channel for new events only in Node.js, skipping stored history and starting from the present.

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 startFrom: EventStoreStartPosition.StartFromNew on the subscription passed to subscribeToEventsStore — 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-sequence position when you need guaranteed replay; and reconnecting doesn't resume where you left off — a fresh StartFromNew subscription starts from "now" again, with no cursor persisted across restarts.

Prerequisites

  • KubeMQ server running on localhost:50000
  • Node.js SDK installed (npm install kubemq-js)

Code

start-new-only.ts
import { KubeMQClient, EventStoreStartPosition, createEventStoreMessage } from 'kubemq-js';

async function main() {
  const client = await KubeMQClient.create({
    address: 'localhost:50000',
    clientId: 'js-events-store-start-new-only-client',
  });

  try {
    const sub = client.subscribeToEventsStore({
      channel: 'js-events-store.start-new-only',
      startFrom: EventStoreStartPosition.StartFromNew,
      onEvent: (event) => {
        console.log(`[seq=${event.sequence}]`, new TextDecoder().decode(event.body));
      },
      onError: (err) => {
        console.error('Error:', err.message);
      },
    });

    // Allow subscription to fully establish on the server.
    await new Promise((resolve) => setTimeout(resolve, 1000));

    await client.sendEventStore(
      createEventStoreMessage({
        channel: 'js-events-store.start-new-only',
        body: 'hello from StartFromNew',
      }),
    );

    await new Promise((r) => setTimeout(r, 1000));
    sub.cancel();
  } finally {
    await client.close();
  }
}

main().catch(console.error);

How It Works

  • StartFromNew skips all previously stored events and delivers only events published after this subscription registers on the server.
  • The 1-second wait is important: it ensures the subscription stream is fully established before the event is published — without it the event might be missed.
  • Unlike regular events, the event is still persisted in the store; a future subscriber using StartFromFirst would receive it in the replay.
  • StartFromNew is the default choice when you only care about live delivery and do not need to process historical events.

Was this page helpful?

On this page