# Start from Last (/sdks/nodejs/how-to/events-store/start-from-last)



## Overview [#overview]

A subscriber that just restarted usually doesn't need the entire event history — it needs to know *where things stand right now* without paying the cost of replaying everything that happened while it was offline. `EventStoreStartPosition.StartFromLast` solves that: it re-anchors a new subscription to the tail of the store, delivering exactly one historical event (the most recently stored one) before switching to live delivery. That's the sweet spot between `StartFromNew` (no history at all, so you might miss the current state entirely) and `StartFromFirst` (the full backlog, which can be slow and mostly irrelevant for a consumer that only cares about "now").

Under the hood, `startFrom: EventStoreStartPosition.StartFromLast` is passed to `subscribeToEventsStore`. The broker looks up the channel's most recent stored event at subscription time, replays that single event to the new subscriber, and then streams every subsequently published event as it arrives — the same live path any other subscription uses.

**Gotchas:** if the channel is empty when you subscribe, there's no "last" event to deliver — you simply start receiving new events as they're published, with no error raised (equivalent to `StartFromNew`). `StartFromLast` gives you one event, not the last N — if you need a short window of recent history, replay from a sequence number instead. And because "last" is resolved at subscribe time, two subscribers starting a few events apart can each get a different one.

## Prerequisites [#prerequisites]

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

## Code [#code]

```typescript title="start-from-last.ts"
import { KubeMQClient, EventStoreStartPosition, createEventStoreMessage } from 'kubemq-js';

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

  try {
    await client.sendEventStore(
      createEventStoreMessage({
        channel: 'js-events-store.start-from-last',
        body: 'pre-existing message',
      }),
    );

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

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

main().catch(console.error);

```

## How It Works [#how-it-works]

* `StartFromLast` delivers only the single most recent stored event, then continues with live delivery of new events — useful for getting the current state without replaying full history.
* One "pre-existing" event is published before the subscription, so `StartFromLast` delivers exactly that one event (the last at subscription time).
* This differs from `StartFromNew`, which skips all history entirely and only delivers events published after the subscription registers.
* If the channel has no events at all when `StartFromLast` is used, the subscription starts in live mode (equivalent to `StartFromNew`).

## Related [#related]

* [Pattern overview](/learn/events-store/getting-started)
* [Node.js SDK Reference](/sdks/nodejs/reference)
* [Persistent Pub/Sub](/sdks/nodejs/tutorials/persistent-pubsub)
* [Cancel Subscription](/sdks/nodejs/how-to/events-store/cancel-subscription)
