AAnvio
Messaging

Local Event Bus

The default, in-process event transport that requires no NATS server — Anvio's Level 1-2 default.

4 min read

This is the default. If you haven't touched events.provider in workspace/anvio.yaml, this is what's running. No server, no network, no Docker.

LocalEventBus (packages/events/src/local-event-bus.ts) implements the exact same EventBusLike interface as the NATS-backed EventBus, but entirely in-process, using a plain JavaScript Map of handler sets. It's what makes anvio chat work standalone in under 5 minutes with zero infrastructure — see the Getting Started overview.

Source

// packages/events/src/local-event-bus.ts
/** In-process event bus for Level 1 — no NATS required. */
export class LocalEventBus {
  private handlers = new Map<string, Set<EventHandler>>();
  private readonly source: string;

  constructor(source = '/anvio/local') {
    this.source = source;
  }

  async connect(): Promise<void> {}   // no-op — nothing to connect to

  async publish<T>(subject: EventSubject, type: string, data: T): Promise<void> {
    await this.publishCore(subject, type, data);
  }

  async publishCore<T>(subject: EventSubject, type: string, data: T): Promise<void> {
    const event = createEvent(type, this.source, data);
    await this.dispatch(subject, event);
  }

  async subscribe<T>(subject: EventSubject, _durableName: string, handler: EventHandler<T>) {
    const unsub = await this.subscribeCore(subject, handler);
    return async () => { unsub(); };
  }

  async subscribeCore<T>(subject: EventSubject, handler: EventHandler<T>) {
    if (!this.handlers.has(subject)) this.handlers.set(subject, new Set());
    this.handlers.get(subject)!.add(handler as EventHandler);
    return () => { this.handlers.get(subject)?.delete(handler as EventHandler); };
  }

  private async dispatch<T>(subject: string, event: AnvioEvent<T>): Promise<void> {
    const handlers = this.handlers.get(subject);
    if (!handlers) return;
    for (const handler of handlers) {
      await handler(event);   // sequential, awaited
    }
  }

  async close(): Promise<void> {
    this.handlers.clear();
  }
}

What's different from NATS JetStream

Local Event BusNATS JetStream
TransportIn-memory Map<subject, Set<handler>>Network connection to a NATS server
PersistenceNone — events are gone the instant dispatch() returnsStream-retained (durable consumers can replay)
durableName parameterAccepted but ignoredUsed to identify a durable consumer, resumable across restarts
Delivery orderSequential for...of, awaited one handler at a timeExplicit ack/nak per message, JetStream handles ordering
Cross-process deliveryNo — only handlers registered in the same Node.js process receive eventsYes — any process connected to the same NATS server
publish() vs publishCore()Identical — publish() just calls publishCore()Different: publish() is durable JetStream, publishCore() is fire-and-forget core NATS
Failure handlingA handler throwing propagates up through dispatch() — no automatic retrymsg.nak() triggers JetStream redelivery

Because dispatch is sequential and awaited, a slow or hanging event handler will block delivery to the next handler on the same subject, and will delay whatever called publish() (since publish() awaits dispatch()). This is invisible at small scale but matters if you register many handlers on a hot subject like anvio.agent.run.chunk.v1.

The critical limitation: single process only

Because LocalEventBus is just a Map living in one Node.js process's memory, events never leave that process. If you run the API (apps/api) and the worker (apps/worker) as two separate processes — which is the normal way to run Anvio beyond a single CLI session — and both are configured with events.provider: local, they do not share events. A POST /api/sessions/:id/messages call publishing anvio.agent.run.requested.v1 from the API process's LocalEventBus will never reach a worker process's separate LocalEventBus instance.

Rendering diagram…

This is the single biggest reason to switch to NATS JetStream: the moment you need more than one process to observe the same events, the local bus can't do it. Level 1 usage (a single anvio chat CLI process, or a single API process handling both publish and subscribe in-process) works fine on the local bus — multi-process deployments do not.

Constructing one

createEventBus() picks LocalEventBus whenever provider !== 'nats':

// packages/events/src/local-event-bus.ts
export async function createEventBus(provider: string, options: { url?: string; source: string }) {
  if (provider === 'nats') {
    const { EventBus } = await import('@anvio/events');
    const bus = new EventBus({ url: options.url ?? 'nats://localhost:4222', source: options.source });
    await bus.connect();
    return bus;
  }
  return new LocalEventBus(options.source);
}

This is the default path taken by createPlatform() when workspace/anvio.yaml has:

spec:
  events:
    provider: local   # or simply omit `events` entirely

When to move off it

SignalAction
Single CLI session or single API processStay on the local bus — it's the right choice
Running apps/api and apps/worker as separate processesSwitch to NATS JetStream
Need events to survive a process restartSwitch to NATS (durable consumers)
Scaling to Level 3/4 (multi-user, distributed)Switch to NATS
Edit on GitHub

Last updated on

On this page